我有两个数组:
$haystack
Array (
[rowid] => Array
(
[0] => 200
[1] => 400
[2] => 500
)
[description] => Array
(
[0] => text1
[1] => text2
[2] => text3
)
[qty] => Array
(
[0] => 1
[1] => 20
[2] => 1
)
)
$needle
Array
(
[rowid] => Array
(
[0] => 200
[1] => 500
)
[description] => Array
(
[0] => newtext1
[1] => newtext3
)
[qty] => Array
(
[0] => 50
[1] => 60
)
)
我想穿过大海捞针阵列(带有foreach)并找到针["rowid"]在大海捞针中存在或不存在的位置。我想得到这样的东西:
干草堆值 200 描述文本 1 存在于针中,但已修改 在 newtext1 中,数量 = 50(针键 0)
缺少干草堆值 400
干草堆值 500 描述文本 3 存在于针中,但经过修改 在数量 = 60 的新文本 3 中(针键 2)
我试过这个:
foreach ($haystack["rowid"] as $key => $value) :
$result = recursive_array_search($haystack["rowid"][$key],$needle);
if ($result) {
//found the same rowid
}
else {
//not found
}
function recursive_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
return array("key" => $current_key, "value" => $value);
}
}
return false;
}
但是当 $key = 2(最后$haystack键)时$result["值"] 或 $result["键"] 变为"null"$needle因为它只有 0 和 1 个键!如何编辑函数?非常感谢,我不是多维数组的专家!
使用新的索引$i :)简单地解决
$i=0;
foreach ($haystack["rowid"] as $key => $value) :
$result = recursive_array_search($haystack["rowid"][$key],$needle["rowid"]);
if ($result) {
//found new value in $needle["description"][$i]
$i++;
}
else {
//not found
}
有没有快速的方法?再见