删除两个嵌套多维数组的重复值
Delete duplicates of values of two nested multidimensional arrays
我正在努力使用 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 )
)
例如,我尝试过使用 this 修改函数的其他解决方案方法,但似乎没有任何效果。有人可以帮助我吗?
你说得对,你需要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;
}
});
我正在努力使用 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 )
)
例如,我尝试过使用 this 修改函数的其他解决方案方法,但似乎没有任何效果。有人可以帮助我吗?
你说得对,你需要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;
}
});