0

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.

4
  • Use json_decode($apiResponse, true); to work with an associative array Commented Jan 4, 2021 at 17:49
  • @jibsteroos Whats that going to achieve, what have you got against Objects Commented Jan 4, 2021 at 17:50
  • Can you show us the raw JSON String $apiResponse please, then we can decode it and test an answer Commented Jan 4, 2021 at 17:53
  • @cmaresh why did you delete your answer, it looks good to me Commented Jan 4, 2021 at 18:03

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!!!!! I had tried the $value->data[0]->rank_details->rank; (forgetting to add the index).

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.