PHP foreach with Nested Array?
我有一个嵌套数组,我想在其中显示结果的子集。例如,在下面的数组中,我想遍历嵌套数组[1]中的所有值。
Array ( [0] => Array ( [0] => one [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) [1] => Array ( [0] => two [1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) ) [2] => Array ( [0] => three [1] => Array ( [0] => 7 [1] => 8 [2] => 9 ) ) )
我试图使用foreach函数,但我似乎无法让它工作。这是我的原始语法(尽管我意识到这是错误的)。
$tmpArray = array(array("one",array(1,2,3)),array("two",array(4,5,6)),array("three",array(7,8,9))); foreach ($tmpArray[1] as $value) { echo $value; }
我试图避免变量比较键是否与我要搜索的键相同,即
foreach ($tmpArray as $key => $value) { if ($key == 1) { echo $value; } }
有什么想法吗?