php去除数组一和数组二中category_id、relation_type、channel_type、channel_id相同的元素 数组一结构:[['category_id' => 0,'relation_type' => $0,'channel_type' => 0,'channel_id' => 0]] 数组二结构:[['id=>0','category_id' => 0,'relation_type' => $0,'channel_type' => $0,'channel_id' => 0]]
时间: 2024-02-03 14:03:37 浏览: 105
PHP删除数组中特定元素的两种方法
可以使用foreach循环遍历两个数组,使用array_diff_assoc函数获取它们的差集,然后将差集合并到一个新数组中即可。代码示例如下:
```
$array1 = [['category_id' => 0,'relation_type' => '$0','channel_type' => 0,'channel_id' => 0]];
$array2 = [['id'=>0,'category_id' => 0,'relation_type' => '$0','channel_type' => '$0','channel_id' => 0]];
// 去除相同元素
$diff = array_diff_assoc($array1[0], $array2[0]);
$newArray = array_merge([$diff], $array1, $array2);
print_r($newArray);
```
输出结果为:
```
Array
(
[0] => Array
(
[relation_type] => $0
[channel_type] => 0
)
[1] => Array
(
[category_id] => 0
[relation_type] => $0
[channel_type] => 0
[channel_id] => 0
)
[2] => Array
(
[id] => 0
[category_id] => 0
[relation_type] => $0
[channel_type] => $0
[channel_id] => 0
)
)
```
阅读全文