PHP 8 reference · Array Functions
PHP array_diff() Function
Return the values of the first array that are not in any of the others. Values are compared as strings.
PHP 8 Since 4.0
Syntax
array_diff(array $array, array ...$arrays): array
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
arrays |
Arrays whose values are excluded. |
Return value
The values only in the first array (keys kept).
Examples
<?php
print_r(array_diff(["a", "b", "c", "d"], ["b", "d"]));
Output
Array
(
[0] => a
[2] => c
)<?php
$before = ["tea", "cake", "milk"];
$after = ["tea", "milk", "jam"];
echo "removed: ", implode(",", array_diff($before, $after)), "\n";
echo "added: ", implode(",", array_diff($after, $before)), "\n";
Output
removed: cake added: jam
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
set.difference()· Python
Related
array_diff_key()· Return the entries of the first array whose keys are not in any of the other arrays.array_diff_assoc()· Return the entries of the first array whose key and value pair isn't in any of the other arrays.array_intersect()· Return the values of the first array that are also present in all the other arrays. Keys come from the first array.- PHP Array Functions · tutorial
- PHP Arrays · tutorial