This is one stop global knowledge base where you can learn about all the products, solutions and support features.
(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.
(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)
array_key_exists — Checks if the given key or index exists in the array
array_key_exists(string|int $key, array $array): bool
array_key_exists()
returns
true
if the given
key
is set in the array.
key
can be any value possible for an array index.
key
Value to check.
array
An array with keys to check.
Returns
true
on success or
false
on failure.
Note :
array_key_exists() will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.
Example #1 array_key_exists() example
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
Example #2 array_key_exists() vs isset()
isset()
does not return
true
for array keys that correspond to a
null
value, while
array_key_exists()
does.
<?php
$search_array = array('first' => null, 'second' => 4);
// returns false
isset($search_array['first']);
// returns true
array_key_exists('first', $search_array);
?>
Note :
For backward compatibility reasons, array_key_exists() will also return
true
ifkey
is a property defined within an object given asarray
. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.To check whether a property exists in an object, property_exists() should be used.
(PHP 7 >= 7.3.0, PHP 8)
array_key_first — Gets the first key of an array
array_key_first(array $array): int|string|null
Get the first key of the given
array
without affecting the internal array pointer.
array
An array.
Returns the first key of
array
if the array is not empty;
null
otherwise.
Example #1 Basic array_key_first() Usage
<?php
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$firstKey = array_key_first($array);
var_dump($firstKey);
?>
The above example will output:
string(1) "a"
There are several ways to provide this functionality for versions prior to PHP 7.3.0. It is possible to use array_keys() , but that may be rather inefficient. It is also possible to use reset() and key() , but that may change the internal array pointer. An efficient solution, which does not change the internal array pointer, written as polyfill:
<?php
if (!function_exists('array_key_first')) {
function array_key_first(array $arr) {
foreach($arr as $key => $unused) {
return $key;
}
return NULL;
}
}
?>