1

In the for loop I am trying to count the amount of flights in a nested array decoded from a JSON feed. Unfortunately it only shows 2 flights, when more flights are available. What am I doing wrong?

Json feed example from endpoint

{
  "response": [
    {
      "flight": {
        "number": "6204",
        "iata_number": "HV6204",
        "icao_number": "TRA6204"
      }
    },
    {
      "flight": {
        "number": "7012",
        "iata_number": "TB7012",
        "icao_number": "JAF7012"
      }
    },
    {
      "flight": {
        "number": "6652",
        "iata_number": "HV6652",
        "icao_number": "TRA6652"
      }
    },
    {
      "flight": {
        "number": "1925",
        "iata_number": "W61925",
        "icao_number": "WZZ1925"
      }
    },
    {
      "flight": {
        "number": "5075",
        "iata_number": "W65075",
        "icao_number": "WZZ5075"
      }
    },
    {
      "flight": {
        "number": "4289",
        "iata_number": "W64289",
        "icao_number": "WZZ4289"
      }
    },
    {
      "flight": {
        "number": "7861",
        "iata_number": "W67861",
        "icao_number": "WZZ7861"
      }
    },
    {
      "flight": {
        "number": "3066",
        "iata_number": "FR3066",
        "icao_number": "RYR3066"
      }
    }
  ]
} .    

The PHP code example

<?php    
$url = 'https://api.endpoint'; // path to JSON file
$data = file_get_contents($url); 
$flights = json_decode($data, true); 

for($i=0; $i<=count($flights['response'][0]['flight']['iata_number']); $i++) {
    echo "Flightnumber" . $flights['response'][$i]['flight']["iata_number"] . '<br/>';
}
?>

Any help is very appreciated

3 Answers 3

2

What really should be used here is foreach. With foreach you don't have to care about count() of your array:

$url = 'https://api.endpoint'; // path to JSON file
$data = file_get_contents($url); 
$flights = json_decode($data, true); 

foreach ($flights['response'] as $item) {
    echo $item['flight']['iata_number'] . '<br />';
}

Simple demo is here.

If you still want to use for-loop, it should look like:

for ($i = 0; $i < count($flights['response']); $i++) {
    echo $flights['response'][$i]['flight']['iata_number'] . '<br />';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This helped a lot!
1
for($i=0; $i<count($flights['response'][0]['flight']['iata_number']); $i++) {
    echo "Flightnumber" . $flights['response'][$i]['flight']["iata_number"] . '<br/>';
}

count($flights['response'][0]['flight']['iata_number']) is always going to equal one, so you're only looping twice as would be expected (two flights). My guess is that's supposed to be count($flights['response']) or something similar.

1 Comment

The <= should also be changed to just <
0

You can use array_walk_recursive

 array_walk_recursive($jarr, function($v, $k) use (&$flights){
  if($k == 'iata_number') $flights[] = $v;
});

Live example :- https://3v4l.org/5Rp9K

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.