0
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
)

所以这是我需要帮助的多维数组。所以我的问题是如何使用 foreach 获取 [original_title]。因为我从来没有做过多维 foreach 谁能解释我会很感激。谢谢

4

4 回答 4

4

你只需要一个foreach...

foreach($array['results'] as $subArray){
    echo $subArray['original_title'];
}
于 2013-06-24T22:44:15.273 回答
1

要回答您的问题,您将执行以下操作:

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

根据您发布的内容,并假设您发布的数组位于名为$var.

如果您确实有一个数组数组的数组,例如,像这样:

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

foreach然后你会做一个像这样的多维的事情:

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

还值得注意的是,由于引用的工作方式,使用对象而不是数组来存储这样的数据通常是一个更好的主意。

于 2013-06-24T22:47:21.203 回答
0

尝试这个:

foreach($MyArray as $Name => $Value) {
    echo $Name.' = '.$Value.'<br />';
}
于 2013-06-27T21:23:48.087 回答
0

首先,您的多维数组应类似于:

$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
    )
  )
)

只是访问'original_title'它的$ary['results'][1]['original_title'];. 要对此运行 foreach 循环,它看起来像:

foreach($ary as $value){
  if(is_array($value)){
    foreach($value as $val){
      foreach($val as $i => $v){
        //$i holds key
        //$v holds value
      }
    }
  }
}
于 2013-06-24T22:57:07.940 回答