PHP array_keys() Function
Topic: PHP Array ReferencePrev|Next
Description
The array_keys()
function return all the keys or a subset of the keys of an array.
The following table summarizes the technical details of this function.
Return Value: | Returns an array containing the keys. |
---|---|
Version: | PHP 4+ |
Syntax
The basic syntax of the array_keys()
function is given with:
The following example shows the array_keys()
function in action.
Example
Run this code »<?php
// Sample array
$persons = array("Harry"=>18, "Clark"=>"32", "John"=>24, "Peter"=>32);
// Getting all the keys from the persons array
print_r(array_keys($persons));
?>
Parameters
The array_keys()
function accepts the following parameters.
Parameter | Description |
---|---|
array | Required. Specifies the array to be used. |
value | Optional. If specified, then only keys containing these values are returned. |
strict | Optional. Determines if strict comparison (=== ) should be used during the value search. Possible values are true and false . Default value is false . |
Note: In strict comparison (using strict equality ===
operator) value and data type must be equal, therefore in strict comparison integer 4 is not equal to string "4".
More Examples
Here're some more examples showing how array_keys()
function actually works:
The following example return all the keys from the persons array containing the value 32.
Example
Run this code »<?php
// Sample array
$persons = array("Harry"=>18, "Clark"=>"32", "John"=>24, "Peter"=>32);
// Getting all the keys having value 32
print_r(array_keys($persons, 32));
?>
The following example will return only those key which has integer value 32 using strict comparison. This can be simply done by setting the strict parameter to true
.
Example
Run this code »<?php
// Sample array
$persons = array("Harry"=>18, "Clark"=>"32", "John"=>24, "Peter"=>32);
// Getting all the keys having integer value 32
print_r(array_keys($persons, 32, true));
?>