2
我要检查每个子阵列为item2
如在第一元件(索引0
)的值。 如果是这样,我想返回第二个元素的值(索引1
)。如何从多维数组中获取值,按名称搜索?
这是我的多维数组:
$arr = Array
(
1 => Array
(
0 => 'item',
1 => 3,
2 => 20,
),
2 => Array
(
0 => 'item2',
1 => 1,
2 => 21,
),
7 => Array
(
0 => 'item3',
1 => 4,
2 => 26,
),
20 => Array
(
0 => 'item4',
1 => 1,
2 => 39,
),
22 => Array
(
0 => 'item1',
1 => 10,
2 => 39,
),
23 => Array
(
0 => 'item2',
1 => 11,
2 => 39,
)
);
预期的结果是:[1,11]
我用这个功能,但它不返回所有结果:
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
如果存在的关键是良好的阵列(1,11) – rayn
谢谢伊万编辑只值.. – rayn