我已经ARRAY1这样的:PHP array_keys多个级别
Array
(
[0] => 123
[1] => 456
[2] => 789
)
和阵列2这样
Array
(
[0] => Array
(
[0] => some text
[1] => 888
[2] => some
[3] => text
)
[1] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
[2] => Array
(
[0] => some text
[1] => 999
[2] => some
[3] => text
)
[3] => Array
(
[0] => some text
[1] => Array
(
[1] => 456
[2] => 789
)
[2] => some
[3] => text
)
[4] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
)
我检查仅1.第二阵列的列和寻找从第一匹配值该值阵列。这是我的代码:
$test=array();
$xcol = array_column($array2, 1);
foreach($array1 as $key => $value) {
if(($foundKey = array_keys($xcol, $value)) !== false) {
$rrt=$foundKey;
foreach($rrt as $rte){
$test[]=$array2[$rte];
}
}
}
echo "<pre>";
print_r($test);
echo "</pre>";
这是工作,给我适当的结果,但它不检查所有级别。任何人都可以请指出我做错了什么? 我的输出是:
Array
(
[0] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
[1] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
)
和期望的输出是:
Array
(
[0] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
[1] => Array
(
[0] => some text
[1] => Array
(
[1] => 456
[2] => 789
)
[2] => some
[3] => text
)
[2] => Array
(
[0] => some text
[1] => 123
[2] => some
[3] => text
)
)
您可以使用['recursiveArrayIterator'(http://php.net/manual/en/class.recursivearrayiterator.php) –
http://php.net/manual/en/function.is-array.php – Hackerman
@mister马丁,你能给我一些提示我将如何使用比array_key搜索找到价值,仍然输出其他所有子数组包含?我从来没有使用recursiveArrayIterator之前,所以我有点困惑 – codexy