简体   繁体   中英

Loop Through Nested JSON Response PHP

I'm trying to loop through and return data ('rank' from 'rank_details') from a returned JSON response.

Here is a snippet of the JSON response (what I'm getting from: $array = json_decode($apiResponse); )

(object) array(
   'obj' => 
  array (
    0 => 
    (object) array(
       'name' => 'I\'m a HellRazor (feat. Crucifix)',
       'id' => 13859011,
       'data' => 
      array (
        0 => 
        (object) array(
           'timestp' => '2019-10-27T00:00:00.000Z',
           'score' => 1.9610844011276853,
           'rank_details' => 
          array (
            0 => 
            (object) array(
               'rank' => 191,
               'country' => 'RU',
               'score' => 1.9610844011276853,
               'genre' => 'Country',
            ),
          ),
        ),
        1 => 
        (object) array(
           'timestp' => '2019-12-04T00:00:00.000Z',
           'score' => 14.70808550760029,
           'rank_details' => 
          array (
            0 => 
            (object) array(
               'rank' => 9,
               'country' => 'CH',
               'score' => 14.70808550760029,
               'genre' => 'Country',
            ),
          ),
        ),
        2 => 
        (object) array(
           'timestp' => '2020-03-18T00:00:00.000Z',
           'score' => 13.299189761918104,
           'rank_details' => 
          array (
            0 => 
            (object) array(
               'rank' => 5,
               'country' => 'RU',
               'score' => 13.299189761918104,
               'genre' => 'Country',
            ),
          ),
        ),
        3 => 
        (object) array(
           'timestp' => '2020-07-12T00:00:00.000Z',
           'score' => 19.02841337415393,
           'rank_details' => 
          array (
            0 => 
            (object) array(
               'rank' => 77,
               'country' => 'DE',
               'score' => 19.02841337415393,
               'genre' => 'Country',
            ),
          ),
        ),
        4 => 
        (object) array(
           'timestp' => '2020-10-02T00:00:00.000Z',
           'score' => 2.631257456412845,
           'rank_details' => 
          array (
            0 => 
            (object) array(
               'rank' => 154,
               'country' => 'RU',
               'score' => 2.631257456412845,
               'genre' => 'Country',
            ),
          ),
        ),
        5 => 
        (object) array(
           'timestp' => '2020-10-03T00:00:00.000Z',
           'score' => 1.896575572629275,
           'rank_details' => 
          array (
            0 => 
            (object) array(
               'rank' => 195,
               'country' => 'RU',
               'score' => 1.896575572629275,
               'genre' => 'Country',
            ),
          ),
        ),
      ),
    ),.....

Here is a snippet of my code:

$apiResponse = curl_exec($cc);
$array = json_decode($apiResponse);
foreach ($array as $key => $arrays) { // This will search in the 2 jsons
    foreach($arrays as $key => $value) {     
        echo "\n Record ID:  " . $value->id;        
        echo "\n Record Name:  " . $value->name;         
        echo "\n Record Rank:  " . $value->obj->data->rank_details->rank;
        echo "\n";    
   }
}

Record Name and ID come over fine, but anything not in the "top level" isn't coming over. Any help is GREATLY appreciated.

You have to index into the data and rank_details arrays even if there's only one entry.

This worked for me:

echo "\n Record Rank:  " . $value->data[0]->rank_details[0]->rank;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM