我已經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