[PHP 4, PHP 5, PHP 7, PHP 8]
array_keys — Return all the keys or a subset of the keys of an array
Description
array_keys[array $array
]: array
array_keys[array $array
,
mixed $search_value
, bool $strict
= false
]: array
If a search_value
is specified, then only the keys for that value are returned. Otherwise, all the keys from the array
are returned.
Parameters
array
An array containing keys to return.
search_value
If specified, then only keys containing this value are returned.
strict
Determines if strict comparison [===] should be used during the search.
Return Values
Returns an array of all the keys in array
.
Examples
Example #1 array_keys[] example
The above example will output:
Array [ [0] => 0 [1] => color ] Array [ [0] => 0 [1] => 3 [2] => 4 ] Array [ [0] => color [1] => size ]
See Also
- array_values[] - Return all the values of an array
- array_combine[] - Creates an array by using one array for keys and another for its values
- array_key_exists[] - Checks if the given key or index exists in the array
- array_search[] - Searches the array for a given value and returns the first corresponding key if successful
pat dot leblanc at gmail dot com ¶
11 years ago
It's worth noting that if you have keys that are long integer, such as '329462291595', they will be considered as such on a 64bits system, but will be of type string on a 32 bits system.
for example:
will return on a 64 bits system:
but on a 32 bits system:
I hope it will save someone the huge headache I had :]
Sven [bitcetera.com] ¶
16 years ago
Here's how to get the first key, the last key, the first value or the last value of a [hash] array without explicitly copying nor altering the original array:
phpnet at holodyn dot com ¶
8 years ago
Since 5.4 STRICT standards dictate that you cannot wrap array_keys in a function like array_shift that attempts to reference the array.
Invalid:
echo array_shift[ array_keys[ array['a' => 'apple'] ] ];
Valid:
$keys = array_keys[ array['a' => 'apple'] ];
echo array_shift[ $keys ];
But Wait! Since PHP [currently] allows you to break a reference by wrapping a variable in parentheses, you can currently use:
echo array_shift[ [ array_keys[ array['a' => 'apple'] ] ] ];
However I would expect in time the PHP team will modify the rules of parentheses.
Ian [maxianos at hotmail dot com] ¶
8 years ago
There's a lot of multidimensional array_keys function out there, but each of them only merges all the keys in one flat array.
Here's a way to find all the keys from a multidimensional array while keeping the array structure. An optional MAXIMUM DEPTH parameter can be set for testing purpose in case of very large arrays.
NOTE: If the sub element isn't an array, it will be ignore.
output:
EXAMPLE:
input:
array[
'Player' => array[
'id' => '4',
'state' => 'active',
],
'LevelSimulation' => array[
'id' => '1',
'simulation_id' => '1',
'level_id' => '1',
'Level' => array[
'id' => '1',
'city_id' => '8',
'City' => array[
'id' => '8',
'class' => 'home',
]
]
],
'User' => array[
'id' => '48',
'gender' => 'M',
'group' => 'user',
'username' => 'Hello'
]
]
array[
'Player' => array[],
'LevelSimulation' => array[
'Level' => array[
'City' => array[]
]
],
'User' => array[]
]
zammit dot andrew at gmail dot com ¶
8 years ago
If an array is empty [but defined], or the $search_value is not found in the array, an empty array is returned [not false, null, or -1]. This may seem intuitive, especially given the documentation says an array is returned, but I needed to sanity test to be sure:
Robert C. ¶
6 years ago
Keys from multi dimensional array to simple array
Want to traverse an multi dimensional array and get the keys back in a single dimensional array? This will do the trick:
Outputs:
array [size=4]
'e' => string 'ieio' [length=4]
1 => string 'one' [length=3]
2 => string 'two' [length=3]
0 => string 'zero' [length=4]
array [size=4]
0 => string 'e' [length=1]
1 => int 1
2 => int 2
3 => int 0
sweet dude sweet
----
expected to see:
dude dude dude
Md. Abutaleb ¶
2 years ago