This is one stop global knowledge base where you can learn about all the products, solutions and support features.
(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)
array_intersect — Computes the intersection of arrays
array_intersect(array $array, array ...$arrays): array
array_intersect()
returns an array containing all the values of
array
that are present in all the arguments. Note that keys are preserved.
array
The array with master values to check.
arrays
Arrays to compare values against.
Returns an array containing all of the values in
array
whose values exist in all of the parameters.
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Example #1 array_intersect() example
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>
The above example will output:
Array ( [a] => green [0] => red )
Note : Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. In words: when the string representation is the same.
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
array_intersect_assoc — Computes the intersection of arrays with additional index check
array_intersect_assoc(array $array, array ...$arrays): array
array_intersect_assoc()
returns an array containing all the values of
array
that are present in all the arguments. Note that the keys are also used in the comparison unlike in
array_intersect()
.
array
The array with master values to check.
arrays
Arrays to compare values against.
Returns an associative array containing all the values in
array
that are present in all of the arguments.
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Example #1 array_intersect_assoc() example
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_intersect_assoc($array1, $array2);
print_r($result_array);
?>
The above example will output:
Array ( [a] => green )
In our example you see that only the pair
"a" =>
"green"
is present in both arrays and thus is returned. The value
"red"
is not returned because in
$array1
its key is
0
while the key of "red" in
$array2
is
1
, and the key
"b"
is not returned because its values are different in each array.
The two values from the
key => value
pairs are considered equal only if
(string) $elem1 === (string) $elem2
. In other words a strict type check is executed so the string representation must be the same.
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_intersect_key — Computes the intersection of arrays using keys for comparison
array_intersect_key(array $array, array ...$arrays): array
array_intersect_key()
returns an array containing all the entries of
array
which have keys that are present in all the arguments.
array
The array with master keys to check.
arrays
Arrays to compare keys against.
Returns an associative array containing all the entries of
array
which have keys that are present in all arguments.
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Example #1 array_intersect_key() example
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_key($array1, $array2));
?>
The above example will output:
array(2) { ["blue"]=> int(1) ["green"]=> int(3) }
In our example you see that only the keys
'blue'
and
'green'
are present in both arrays and thus returned. Also notice that the values for the keys
'blue'
and
'green'
differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of
array
.
The two keys from the
key => value
pairs are considered equal only if
(string) $key1 === (string) $key2
. In other words a strict type check is executed so the string representation must be the same.
(PHP 5, PHP 7, PHP 8)
array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function
array_intersect_uassoc(array $array, array ...$arrays, callable $key_compare_func): array
array_intersect_uassoc()
returns an array containing all the values of
array
that are present in all the arguments. Note that the keys are used in the comparison unlike in
array_intersect()
.
array
Initial array for comparison of the arrays.
arrays
Arrays to compare keys against.
key_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
callback(mixed $a, mixed $b): int
Returns the values of
array
whose values exist in all of the arguments.
Example #1 array_intersect_uassoc() example
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));
?>
The above example will output:
Array ( [b] => brown )
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison
array_intersect_ukey(array $array, array ...$arrays, callable $key_compare_func): array
array_intersect_ukey()
returns an array containing all the values of
array
which have matching keys that are present in all the arguments.
array
Initial array for comparison of the arrays.
arrays
Arrays to compare keys against.
key_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
callback(mixed $a, mixed $b): int
Returns the values of
array
whose keys exist in all the arguments.
Example #1 array_intersect_ukey() example
<?php
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
?>
The above example will output:
array(2) { ["blue"]=> int(1) ["green"]=> int(3) }
In our example you see that only the keys
'blue'
and
'green'
are present in both arrays and thus returned. Also notice that the values for the keys
'blue'
and
'green'
differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of
array
.
(PHP 8 >= 8.1.0)
array_is_list
—
Checks whether a given
array
is a list
array_is_list(array $array): bool
Determines if the given
array
is a list. An
array
is considered a list if its keys consist of consecutive numbers from
0
to
count($array)-1
.
array
The array being evaluated.
Returns
true
if
array
is a list,
false
otherwise.
Example #1 array_is_list() example
<?php
array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true
// The array does not start at 0
array_is_list([1 => 'apple', 'orange']); // false
// The keys are not in the correct order
array_is_list([1 => 'apple', 0 => 'orange']); // false
// Non-integer keys
array_is_list([0 => 'apple', 'foo' => 'bar']); // false
// Non-consecutive keys
array_is_list([0 => 'apple', 2 => 'bar']); // false
?>
Note :
This function returns
true
on empty arrays.