How to use foreach function in the the case of arrays of arrays

<>

This question is an exact duplicate of:

Answers


Just one thing... In your echo statement you have $s wrapped in single quotes ('), the variable will not be read unless it's in double quotes ("). ie: ["$s"]. Or you can do [''.$s.''] or just remove the quotes all together [$s].

And now the fix...

foreach($array_data['AvailResponse']['OriginDestinationOptions']['OriginDestinationOption']['0']['onward']['FlightSegments']['FlightSegment'] as $array)
{
    echo $c=$array['FlightNumber'];
}

Not a very elegant solution, but it works.

A link to the code working: http://phpfiddle.org/main/code/k9s-3i1


Try like this

foreach($array_data as $array)
{
    if(is_array($array))
    {
        foreach($array as $arr)
        {
             //Do the stuff
        } 
    }  
     //Do the stuff
}

just check that whether it is an array or not each time.if it is then apply another foreach loop


You are trying to access the data in a known position. I don't see why you have to make a loop.

echo $array_data['AvailResponse']['OriginDestinationOptions']['OriginDestinationOption']['0']['onward']['FlightSegments']['FlightSegment']['$s']['FlightNumber'];


Need Your Help

Trying to get row data alert. Why am I getting multiple alerts on click?

javascript jquery

I am at early stages of learning js and jquery. I am stuck in a problem I dont understand.

play sound while recording fails

ios audio core-audio record

I have a 3rd party SDK that handles an audio recording. It has a callback when the recording starts. In the callback I'm trying to play a sound to indicate to the user that the device is now listen...