The array_diff()
function in PHP 8.4 computes the difference between two or more arrays, comparing their values. It returns an array containing the values from the first array that are not present in any of the subsequent arrays.
This function remains unchanged in PHP 8.4, continuing to work as in previous versions.
Syntax
array_diff(array $array, array …$arrays): array
Parameters
$array
(required): The array to compare.$arrays
(required): One or more arrays to compare against.
Return Value
- Returns an array containing the values from the first array that are not present in any of the other arrays.
- The keys are preserved from the first array.
Example 1: Basic Usage
<?php
$array1 = ["a" => "red", "b" => "green", "c" => "blue"];
$array2 = ["a" => "red", "d" => "yellow"];
$result = array_diff($array1, $array2);
print_r($result);
?>
Output:
Array
(
[b] => green
[c] => blue
)
Explanation: The values "green"
and "blue"
are in $array1
but not in $array2
.
Example 2: Multiple Arrays
<?php
$array1 = ["red", "green", "blue"];
$array2 = ["green", "yellow"];
$array3 = ["blue", "purple"];
$result = array_diff($array1, $array2, $array3);
print_r($result);
?>
Output:
Array
(
[0] => red
)
Explanation: The value "red"
is in $array1
but not in $array2
or $array3
.
Notes
- Comparison is case-sensitive, meaning
"Red"
and"red"
are treated as different values. - The function only compares values, not keys.
PHP 8.4 Context
The array_diff()
function remains unchanged in PHP 8.4. It retains its same functionality and behavior as in earlier versions, ensuring backward compatibility.
If you have specific use cases or concerns about array_diff()
in PHP 8.4, let me know!