The array_keys()
function returns an array of all the keys in that array.
PHP array_keys() function has the following syntax
array array_keys ( array arr [, mixed value [, bool strict]] )
Parameter | Is Required | Description |
---|---|---|
arr | Required. | Array to search |
value | Optional. | Only the keys with this value are returned |
strict | Optional. | Used with the value parameter. |
Possible values for strict:
Value | Description |
---|---|
true | Check type. the number 9 is not the same as the string "9". |
false | Default value. Not checking type, the number 9 is the same as the string "9". |
By default, this is done by checking
each key's value with the ==
operator (is equal to);
however, if you specify 1
as the
third parameter, the check will be done with ===
(identical to).
Get all keys in an array
<?php
$users[0] = 'PHP';
$users[1] = 'Java';
$users[2] = 'java2s.com';
$userids = array_keys($users);
print_r($userids);
?>
The code above generates the following result.
Using the value parameter:
<?php
$a=array("p"=>"PHP","j"=>"java2s.com","a"=>"Apple");
print_r(array_keys($a,"java2s.com"));
?>
The code above generates the following result.
Using the strict parameter, false:
<?php
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",false));
?>
The code above generates the following result.
Using the strict parameter, true:
<?php
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",true));
?>
The code above generates the following result.