问题描述
我有一个嵌套数组,我想在其中显示结果的子集.例如,在下面的数组中,我想遍历嵌套数组 [1] 中的所有值.
大批([0] => 数组([0] => 一[1] => 数组([0] => 1[1] => 2[2] => 3))[1] => 数组([0] => 两个[1] => 数组([0] => 4[1] => 5[2] => 6))[2] => 数组([0] => 三[1] => 数组([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] 作为 $value) {回声 $value;}我试图避免对密钥是否与我要搜索的密钥相同进行变量比较,即
foreach ($tmpArray as $key => $value) {如果 ($key == 1) {回声 $value;}}有什么想法吗?
如果您知道嵌套数组中的级别数,您可以简单地执行嵌套循环.像这样:
//扫描外循环foreach ($tmpArray 作为 $innerArray) {//检查类型如果(is_array($innerArray)){//扫描内循环foreach ($innerArray 作为 $value) {回声 $value;}}别的{//一二三回声 $innerArray;}}
如果您不知道数组的深度,则需要使用递归.请参见下面的示例:
//多维源数组$tmpArray = 数组(数组(一",数组(1, 2, 3)),数组("二", 数组(4, 5, 6)),数组(三",数组(7、8、数组(四", 9, 10))));//输出数组displayArrayRecursively($tmpArray);/*** 用缩进显示数组成员的递归函数** @param array $arr 要处理的数组* @param string $indent 缩进字符串*/函数 displayArrayRecursively($arr, $indent='') {如果($arr){foreach ($arr 作为 $value) {如果 (is_array($value)) {//displayArrayRecursively($value, $indent .'--');} 别的 {// 输出echo "$indent $value \n";}}}}
下面的代码仅显示带有特定情况值的嵌套数组(仅限第 3 级)
$tmpArray = 数组(数组(一",数组(1, 2, 3)),数组("二", 数组(4, 5, 6)),数组(三",数组(7、8、9)));//扫描外循环foreach ($tmpArray 作为 $inner) {//检查类型如果(is_array($inner)){//扫描内循环foreach ($inner[1] 作为 $value) {echo "$value \n";}}}
I have a nested array in which I want to display a subset of results. For example, on the array below I want to loop through all the values in 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 ) ) )
I was trying to use the foreach function but I cannot seem to get this to work. This was my original syntax (though I realise it is wrong).
$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; }
I was trying to avoid a variable compare on whether the key is the same as the key I want to search, i.e.
foreach ($tmpArray as $key => $value) { if ($key == 1) { echo $value; } }
Any ideas?
If you know the number of levels in nested arrays you can simply do nested loops. Like so:
// Scan through outer loop
foreach ($tmpArray as $innerArray) {
// Check type
if (is_array($innerArray)){
// Scan through inner loop
foreach ($innerArray as $value) {
echo $value;
}
}else{
// one, two, three
echo $innerArray;
}
}
if you do not know the depth of array you need to use recursion. See example below:
// Multi-dementional Source Array
$tmpArray = array(
array("one", array(1, 2, 3)),
array("two", array(4, 5, 6)),
array("three", array(
7,
8,
array("four", 9, 10)
))
);
// Output array
displayArrayRecursively($tmpArray);
/**
* Recursive function to display members of array with indentation
*
* @param array $arr Array to process
* @param string $indent indentation string
*/
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $value) {
if (is_array($value)) {
//
displayArrayRecursively($value, $indent . '--');
} else {
// Output
echo "$indent $value \n";
}
}
}
}
The code below with display only nested array with values for your specific case (3rd level only)
$tmpArray = array(
array("one", array(1, 2, 3)),
array("two", array(4, 5, 6)),
array("three", array(7, 8, 9))
);
// Scan through outer loop
foreach ($tmpArray as $inner) {
// Check type
if (is_array($inner)) {
// Scan through inner loop
foreach ($inner[1] as $value) {
echo "$value \n";
}
}
}
这篇关于带有嵌套数组的 PHP foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!