Get all values ​​from JSON array

Hi anyone help me how to get all the value of "clickUrl", I am using file_get_content function and I want to wrap it in html

<?php 
$json_url = "someone.web.id";
$json = file_get_contents($json_url);
$json=str_replace('},]',"}]",$json);
$data = json_decode($json);

echo "<pre>";
print_r($data);
echo "</pre>";
?>
      

Run codeHide result


My result

stdClass Object
(
    [serialId] => 110153837
    [productId] => 212065382
    [os] => Linux
    [validResponse] => 1
    [errorMessage] => 
    [adsDetails] => Array
        (
            [0] => stdClass Object
                (
                    [requestTimestamp] => 1429665317403
                    [adId] => SAP_272131
                    [clickUrl] => http://adtrack.king.com
                 )
             [1] => stdClass Object
                (
                    [requestTimestamp] => 1429679659674
                    [adId] => SAP_182149
                    [clickUrl] => market://details?id    
                 )
             [2] => stdClass Object
                (
                    [requestTimestamp] => 1429679659674
                    [adId] => SAP_552219
                    [clickUrl] => http://t.mobitrk.com
                )   
            [3] => stdClass Object
                (
                    [requestTimestamp] => 1429679659674
                    [adId] => SAP_562515
                    [clickUrl] => https://app.adjust.io
                )
        )
)
    
      

Run codeHide result


I don't know why my early post was removed by meagar

+3


source to share


2 answers


Move from stdobject to and around the array.

To get JSON as an array, just change this:

$data = json_decode($json);

      

to This:



$data = json_decode($json,true);

      

Then scroll the circle ...

foreach ($data['adsDetails'] as $innerArray) {
   echo $innerArray['requestTimestamp'].'<br>';
   echo $innerArray['adId'].'<br>';
   echo $innerArray['clickUrl'].'<br>';
}

      

+2


source


According to your information, you can get an array:



$clickUrls = array_map(function($val) {
  return $val->clickUrl;
}, $data->adsDetails);

      

+4


source







All Articles