从JSON数组中获取所有值

时间:2015-04-22 05:22:53

标签: php arrays json

大家会帮助我如何获得所有价值" clickUrl",  我正在使用file_get_content函数,我想将其包装在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>";
?>
&#13;
&#13;
&#13;

我的结果是

&#13;
&#13;
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
                )
        )
)
    
&#13;
&#13;
&#13;

我不知道为什么我的早期帖子被meagar删除了

2 个答案:

答案 0 :(得分:4)

根据您的信息,您可以通过以下方式获得阵列:

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

答案 1 :(得分:2)

从stdobject更改为数组并在其周围循环。

要将JSON作为数组接收,只需更改:

$data = json_decode($json);

到此:

$data = json_decode($json,true);

然后,循环......

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