【发布时间】:2015-03-21 21:45:16
【问题描述】:
我有这 2 个数组:
$haystack = array("rowid" => array("100", "200"), "description" => array ("something", "something else"));
$needle = array("rowid" => array("200", "300", "400"), "description" => array ("something else", "other", "other else"), "test" => "bye");
我想用 $needle 和 $haystack 做一个 array_diff,但我得到一个错误,因为 $needle 数组是多维的,但“测试”键只有一维:
foreach ($needle as $key => $value) :
$left[$key] = array_diff($needle[$key],$haystack[$key]);
endforeach;
警告:array_diff():参数 #1 不是数组
警告: array_diff(): 参数 #2 不是数组
我该如何解决这个问题? 我只需要在 $left 数组中维护“rowid”和“description”值(我可以丢弃“test”值)。
$left 应该是:
Array
(
[rowid] => Array
(
[0] => 300
[1] => 400
)
[description] => Array
(
[0] => other
[1] => other else
)
)
感谢您的帮助!
【问题讨论】:
-
你遍历你的数组并尝试
array_diff每个元素。但是我们最后一个test-value 不是数组。所以你会收到警告。在使用is_array()将元素传递给函数之前检查元素
标签: php arrays search multidimensional-array array-difference