php 二维数组差集
时间: 2023-12-13 20:33:27 浏览: 142
求数组的差集.cpp
以下是PHP二维数组差集的两种方法:
方法一:使用array_udiff()函数
```php
function compare($a, $b) {
return $a['id'] - $b['id'];
}
$arr1 = array(
array('id' => 101, 'name' => 'Tom', 'age' => 20),
array('id' => 102, 'name' => 'ptbird', 'age' => 19),
array('id' => 103, 'name' => 'Jerry', 'age' => 18)
);
$arr2 = array(
array('id' => 101, 'name' => 'Tom', 'age' => 20),
array('id' => 103, 'name' => 'Jerry', 'age' => 18)
);
$result = array_udiff($arr1, $arr2, 'compare');
print_r($result);
```
方法二:使用array_filter()函数
```php
function diff($a, $b) {
return $a['id'] - $b['id'];
}
$arr1 = array(
array('id' => 101, 'name' => 'Tom', 'age' => 20),
array('id' => 102, 'name' => 'ptbird', 'age' => 19),
array('id' => 103, 'name' => 'Jerry', 'age' => 18)
);
$arr2 = array(
array('id' => 101, 'name' => 'Tom', 'age' => 20),
array('id' => 103, 'name' => 'Jerry', 'age' => 18)
);
$result = array_filter($arr1, function($a) use ($arr2, $diff) {
return count(array_filter($arr2, function($b) use ($a, $diff) {
return $diff($a, $b) == 0;
})) == 0;
});
print_r($result);
```
阅读全文