循环遍历多个对象json数据

时间:2018-01-30 10:58:34

标签: php json

我正在尝试循环访问JSON数据以获取" cell"的值。 &安培;相应的"标签"。坚持如何循环多个数组和对象

<?php
$url = file_get_contents("data.json");
$results = json_decode($url, true);
foreach ($results as $key => $value) {

}
?>

JSON数据https://jsfiddle.net/6ft3vx35/

[null,null,{"rows":[{"cells":[{"value":"755","formatted_value":"755"}],"label":"Twitter"},{"cells":[{"value":"151","formatted_value":"151"}],"label":"Online News"},{"cells":[{"value":"107","formatted_value":"107"}],"label":"Blogs"},{"cells":[{"value":"28","formatted_value":"28"}],"label":"Newspaper"},{"cells":[{"value":"17","formatted_value":"17"}],"label":"Facebook"},{"cells":[{"value":"10","formatted_value":"10"}],"label":"Instagram"},{"cells":[{"value":"2","formatted_value":"2"}],"label":"Forums"},{"cells":[{"value":"2","formatted_value":"2"}],"label":"TV/Radio"},{"cells":[{"value":"1","formatted_value":"1"}],"label":"Flickr"},{"cells":[{"value":"1","formatted_value":"1"}],"label":"News Agency"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Press Release"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Magazine"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Radio Broadcast"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"TV Broadcast"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Print Other"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Local Weekly"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Papers Regional \u0026 Local Newspapers"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"National Newspapers"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Print News"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Print Magazine"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"External"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Douban"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Vine"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Pinterest"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"VKontakte"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Weibo"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Dailymotion"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Vimeo"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Soundcloud"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Mixcloud"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Foursquare"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"LinkedIn"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"YouTube"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Google+"},{"cells":[{"value":"0","formatted_value":"0"}],"label":"Newsletter"}],"column_groups":[{"data_columns":[{"name":"Results","code":"COUNT","type":"number"}]}],"column_type":"","row_type":"","total_results":"1.1K","layers":[]}]

2 个答案:

答案 0 :(得分:1)

您需要像这样迭代数组

<?php
$url = file_get_contents("data.json");
$results = json_decode($url, true);
$rows = $results[2];
foreach ($rows as $row) {
    $label = $row["label"];
    foreach ($row["cells"] as $cell) {
         $value = $cell["value"];

         // do some stuff with $label and $value
    }
}
?>

答案 1 :(得分:1)

您可以遍历foreach()

foreach($results[2]['rows'] as $res){
   echo 'label is:-'.$res['label'].PHP_EOL;
   echo 'Cells Value is:-'.$res['cells'][0]['value'].PHP_EOL;
   echo 'Cells Formatted Value is:-'.$res['cells'][0]['formatted_value'].PHP_EOL;
}

示例输出: -

1。https://eval.in/944780

2。https://eval.in/944788