1.函数的作用:比较集合间的元素,返回数组的差值;
2.函数的参数:
@params array $array1 被比较的数组
@params array $array2 比较的数组
@params array $array3 比较的数组
...
3.注意点:
1)比较方法 : (string) $element1 === (string)$element2
4.例子:
例一:
1 <?php 2 $array1 = ['apple','pear','pie']; 3 $array2 = ['pie']; 4 print_r(array_diff($array1,$array2));
1 <?php 2 $array1 = ['apple','pear','pie']; 3 $array2 = ['pie','banana']; 4 print_r(array_merge(array_diff($array1,$array2),array_diff($array2,$array1)));
例二:
1 <?php 2 class User 3 { 4 public $name = "Hello"; 5 public $sex = null; 6 } 7 print_r(array_diff((array)(new User()),[null]));
输出:
例三:
1 <?php 2 /* 3 * http://php.net/manual/zh/function.array-diff.php 4 * pass value you wish to delete and the array to delete from 5 */ 6 function array_delete( $value, $array) 7 { 8 $array = array_diff( $array, array($value) ); 9 return $array; 10 }