Garg Garg - 2 months ago 6
JSON Question

Searching in json array for parameter and show correct information

Is it possible to search inside json array which I pull with curl and if match to show only that data?

The array looks like

Array
(
[status] => success
[data] => Array
(
[out] => Array
(
[0] => Array
(
[address] => test address
[amount] => 11
[type] => 1
)

[1] => Array
(
[address] => test address 1
[is_nonstandard] =>
[amount] => 12
[type] => 1

)

)
)

[code] => 200
[message] =>
)


And this is how I pull the array

function get_curl_content_tx($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$url=get_curl_content_tx("http://example.com");
$total =json_decode($url,true);


So since there can be more that 1 arrays in
[out] => Array
as current situation they are two I want to search and match by
[address]
and when match to give data.

Answer

Here is a way to do it :

foreach($arr['data']['out'] as $d){
    if(strpos($d['address'], 'testaddress') !== false){
        //do something
        var_dump($d);
    }
}

You need to replace var_dump by whatever you want to do. I used strpos for the sake of this example, but you might want to use a custom method that would suits your needs better

Hope this helps.