Definition and usage
The Array_diff () function returns an array of difference sets for two arrays. The elements of the returned array are taken from the array being compared (both the first array).
In the returned array, the key name remains unchanged.
Grammar
Array_diff (Array1,array2,array3 ...)
| Parameters |
Describe |
| Array1 |
Necessary. The first array to compare with the other array. |
| Array2 |
Necessary. The array to compare with the first array. |
| Array3 |
Optional. The array to compare with the first array. |
Hints and Notes
Tip: You can use one or any number of arrays to compare to the first array.
Note: Use only values to compare.
Example 1
<?php$a1 = Array (0=> "A",1=> "B",2=> "C"), $a 2 = Array (3=> "C",4=> "D",5=> "B");p Rint_r (Array_diff ($ A1, $a 2));? >
Output:
Array ([0] = A)
Example
<?php//Most groups compare $ A = Array (1,2,3,7), $b = Array (4,3,5), $c = Array (2,6,8);p Rint_r (Array_diff ($a, $b, $c)); >
Output:
The 1,7 in array $ A does not exist in the latter two arrays ($b, $c) array ([0] = 1 [3] = 7)
PHP Array_diff ()