1

I am trying to figure out how to parse a json response that has multiple results. Here is the response data -

 Array
 (
    [id] => 7181676
    [api_request_id] => 20984853
    [user] => 8305
    [vin] => JTDKN3DU7D1643423
    [make] => Toyota
    [model_name] => Prius
    [model_year] => 2013
    [last_updated] => 2019-02-22T01:08:15.628318Z
    [recall_count] => 2
    [status] => ok
    [recalls] => Array
        (
            [0] => Array
                (
                    [recall_id] => 15753
                    [recall_age] => 0
                    [nhtsa_id] => 18V684000

                )
            [1] => Array
                (
                    [recall_id] => 16733
                    [recall_age] => 1
                    [nhtsa_id] => 16V684123

                )
        )
)

I would like to loop through to get the recalls[0] and recalls[1] as I wont know how many results there will be so cant do it manually.

The response I am looking for is -

2013 Toyota Prius

Recall ID - 1573
Recall Age - 0
Nhtsa Id - 18v684000

Recall ID - 16733
Recall Age - 1
Nhtsa Id - 16v684123

Would I do something like this? -

$data = curl_exec($ch);
$responseData = json_decode($data, TRUE);

foreach($responseData as $item){
    echo $item;
}
4
  • You can do like echo $item['id']; to get it's corresponding value which is 7181676 Commented Feb 26, 2019 at 5:15
  • Yes thank you I know how I can get the individual values but since there will be an unknown amount of responses I would like to loop through each $responseDara[recalls][0], $responseDara[recalls][1] etc.. Commented Feb 26, 2019 at 5:19
  • 1
    Just check the array while iterating the array after parsing the json data using is_array() method. Commented Feb 26, 2019 at 5:19
  • recall_count tells me if there is more than one result, I could use this possibly to know how many times to iterate but not sure on how that would be executed. Commented Feb 26, 2019 at 5:21

2 Answers 2

2

You can just loop over the recalls array in your data; by using a foreach loop you don't have to worry how many there are in the array:

foreach ($responseData['recalls'] as $key => $recall) {
    echo "recall $key:\n";
    echo "recall id: {$recall['recall_id']}\n";
    echo "recall age: {$recall['recall_age']}\n";
    echo "nhtsa id: {$recall['nhtsa_id']}\n\n";
}

Output:

recall 0: 
recall id: 15753
recall age: 0 
nhtsa id: 18V684000 

recall 1: 
recall id: 16733
recall age: 1 
nhtsa id: 16V684123

Demo on 3v4l.org

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

2 Comments

This works perfectly cheers mate! The as $key => $recall breaks out each iteration by the array key I take it? [0], [1] etc..?
@RyanD yes, $key gets the array index from the recalls array, and $recall gets the contents of that element (in itself another array with 3 elements, recall_id, recall_age, and nhtsa_id)
1

loop through the array via for loop for example:

for ($i = 0; $i < count($responseData['recalls']); $i++) {
    $result = $responseData['recalls'][$i];
    echo $result['recall_id'].'<br>';
    echo $result['recall_age'].'<br>';
    echo $result['nhtsa_id'].'<br>';
}

Comments

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.