我正在努力array_diff解决我的问题的功能和另一种解决方案,但没有任何东西给我想要的效果..我有两个从数据库返回的多维数组(嵌套深度未知)并将编码和解码为 JSON。数组如下所示:Array ( [0] => Array ( [id] => 1 [lft] => 1 [rgt] => 2 ) [1] => Array ( [id] => 2 [lft] => 3 [rgt] => 4 ) [2] => Array ( [id] => 3 [lft] => 5 [rgt] => 6 ) [3] => Array ( [id] => 4 [lft] => 7 [rgt] => 16 ) [4] => Array ( [id] => 5 [lft] => 8 [rgt] => 9 ) [5] => Array ( [id] => 6 [lft] => 10 [rgt] => 11 ) [6] => Array ( [id] => 7 [lft] => 12 [rgt] => 15 ) [7] => Array ( [id] => 8 [lft] => 13 [rgt] => 14 ) [8] => Array ( [id] => 9 [lft] => 17 [rgt] => 18 ))Array ( [0] => Array ( [id] => 17 [lft] => 1 [rgt] => 2 ) [1] => Array ( [id] => 3 [lft] => 3 [rgt] => 10 ) [2] => Array ( [id] => 9 [lft] => 4 [rgt] => 9 ) [3] => Array ( [id] => 24 [lft] => 5 [rgt] => 6 ) [4] => Array ( [id] => 81 [lft] => 7 [rgt] => 8 ))现在我想合并这两个数组,以便我得到第一个数组,没有与第二个数组中的元素具有相同 id 的元素。结果应该是这样的:Array ( [0] => Array ( [id] => 1 [lft] => 1 [rgt] => 2 ) [1] => Array ( [id] => 2 [lft] => 3 [rgt] => 4 ) [3] => Array ( [id] => 4 [lft] => 7 [rgt] => 16 ) [4] => Array ( [id] => 5 [lft] => 8 [rgt] => 9 ) [5] => Array ( [id] => 6 [lft] => 10 [rgt] => 11 ) [6] => Array ( [id] => 7 [lft] => 12 [rgt] => 15 ) [7] => Array ( [id] => 8 [lft] => 13 [rgt] => 14 ))例如,我尝试过使用这个修改过的函数使用其他解决方案,但似乎没有任何效果。有谁能够帮助我?
1 回答
慕慕森
TA贡献1856条经验 获得超17个赞
你是对的,你需要array_udiff。
尝试这个:
$res = array_udiff($array1, $array2, function ($a, $b) {
if ($a['id'] < $b['id']) {
return -1;
} elseif ($a['id'] > $b['id']) {
return 1;
} else {
return 0;
}
});
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报
0/150
提交
取消