简体   繁体   中英

PHP multidimensional foreach loop

Array
(
[page] => 1
[results] => Array
    (
        [0] => Array
            (
                [adult] => 
                [backdrop_path] => /9rZg1J6vMQoDVSgRyWcpJa8IAGy.jpg
                [id] => 680
                [original_title] => Pulp Fiction
                [release_date] => 1994-10-14
                [poster_path] => /dM2w364MScsjFf8pfMbaWUcWrR.jpg
                [popularity] => 6.4840769584183
                [title] => Pulp Fiction
                [vote_average] => 7.8
                [vote_count] => 942
            )

        [1] => Array
            (
                [adult] => 
                [backdrop_path] => 
                [id] => 109005
                [original_title] => Pulp Fiction Art
                [release_date] => 2005-01-01
                [poster_path] => /yqbnCy6YCc9VY8cnkHGEgiylXey.jpg
                [popularity] => 0.2
                [title] => Pulp Fiction Art
                [vote_average] => 0
                [vote_count] => 0
            )

    )

[total_pages] => 1
[total_results] => 2
)

So this is my multidimensional array that i need help with. So my problem is how to get [original_title] with foreach. Because i never done a multidimensional foreach can someone explain me would be grateful. Thanks

You only need one foreach...

foreach($array['results'] as $subArray){
    echo $subArray['original_title'];
}

To answer your question, you would do something like this:

foreach($var['results'] as $result)
{
    echo $result['original_title'];
}

Based upon what you have posted, and assuming that the array you posted is in a variable called $var .

If you did have a situation where you had an array of arrays of arrays, for example, like this:

Array (
    Array(
        Array(
            "title" => "foo",
            "data" => "bar"
        ),
        ...
    ),
    ...
)

Then you would do a multi-dimensional foreach something like this:

foreach($var as $inner)
{
    foreach($inner as $innerInner)
    {
        echo $innerInner['title'];
    }
}

It's also worth noting that, because of how references work, it's generally a much better idea to use objects instead of arrays for storing data like this.

First of all your Multidimensional Array should look something like:

$ary = array(
  'page' => 1,
  'results' => array(
    array(
      'adult' => null,
      'backdrop_path' => '/9rZg1J6vMQoDVSgRyWcpJa8IAGy.jpg',
      'id' => 680,
      'original_title' => 'Pulp Fiction',
      'release_date' => '1994-10-14',
      'poster_path' => '/dM2w364MScsjFf8pfMbaWUcWrR.jpg',
      'popularity' => 6.4840769584183,
      'title' => 'Pulp Fiction',
      'vote_average' => 7.8,
      'vote_count' => 942
    ),
    array(
      'adult' => null, 
      'backdrop_path' => null,
      'id' => 109005,
      'original_title' => 'Pulp Fiction Art',
      'release_date' => '2005-01-01',
      'poster_path' => '/yqbnCy6YCc9VY8cnkHGEgiylXey.jpg',
      'popularity' => 0.2,
      'title' => 'Pulp Fiction Art',
      'vote_average' => 0,
      'vote_count' => 0
    )
  )
)

To just access 'original_title' it's $ary['results'][1]['original_title']; . To run a foreach loop on this it would look like:

foreach($ary as $value){
  if(is_array($value)){
    foreach($value as $val){
      foreach($val as $i => $v){
        //$i holds key
        //$v holds value
      }
    }
  }
}

Try this:

foreach($MyArray as $Name => $Value) {
    echo $Name.' = '.$Value.'<br />';
}

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