Php curl result in JSON get values

following is my code for php curl where i get values of currency conversion.

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://free.currencyconverterapi.com/api/v5/convert?q=USD_PKR&compact=y" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $ch );
$json = json_decode($response, true);
print_r($json);

it gives me result

Array ( [USD_PKR] => Array ( [val] => 131.1098 ) )

I want to put 131.1098 into a variable where I will update a sql table query. Kindly advice

2 answers

  • answered 2018-10-11 19:37 sietse85

    $number = $json['USD_PKR']['val'];
    

  • answered 2018-10-11 19:54 Salman Razak

    and how about following will it show the result?

    $myarray = array('USD_PKR','USD_GBR','USD_EUR');
    $array_count = count($myarray);
    
    for($i=0;$i<$array_count;$i++){
        echo $myarray[$i];
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, "http://free.currencyconverterapi.com/api/v5/convert?q=".$myarray[$i]."&compact=y" );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        $response = curl_exec( $ch );
        $json = json_decode($response, true);
        $values = $json[$i]['val'];
        echo $values;
    }