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_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;
}
}
?>
(PHP 7 >= 7.3.0, PHP 8)
array_key_last — Gets the last key of an array
array_key_last(array $array): int|string|null
Get the last key of the given
array
without affecting the internal array pointer.
array
An array.
Returns the last key of
array
if the array is not empty;
null
otherwise.
(PHP 4, PHP 5, PHP 7, PHP 8)
array_keys — Return all the keys or a subset of the keys of an array
array_keys(array $array): array
array_keys(array $array, mixed $filter_value, bool $strict = false): array
array_keys()
returns the keys, numeric and string, from the
array
.
If a
filter_value
is specified, then only the keys for that value are returned. Otherwise, all the keys from the
array
are returned.
array
An array containing keys to return.
filter_value
If specified, then only keys containing this value are returned.
strict
Determines if strict comparison (===) should be used during the search.
Returns an array of all the keys in
array
.
Example #1 array_keys() example
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>
The above example will output:
Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size )