0

I am trying to loop through JSON data to get values of "cell" & corresponding "label". Stuck on how to loop multiple array and objects

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

}
?>

JSON data 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":[]}]
6
  • 1
    What is your question? Commented Jan 30, 2018 at 10:59
  • 1
    Yes what is the actual problem that you wanted to ask please add some more detail to your question Commented Jan 30, 2018 at 11:00
  • He is trying to loop through the decoded JSON, I think that's obvious, OP do a var_dump($results) Commented Jan 30, 2018 at 11:01
  • 1
    He is trying to loop through the decoded JSON.. I've already read this... @MehdiBounya Commented Jan 30, 2018 at 11:02
  • @B001 I don't get what you mean Commented Jan 30, 2018 at 11:03

2 Answers 2

2

You can iterate through 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;
}

Sample Output:-

1.https://eval.in/944780

2.https://eval.in/944788

Sign up to request clarification or add additional context in comments.

Comments

1

You need to iterate array like this

<?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 Comment

You could do some explanation? a pure code answer isn't helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.