Definition and Usage
The array_keys() function returns an array containing the keys.Syntax
array_keys(array,value)
| Parameter | Description |
|---|---|
| array | Required. Specifies an array |
| value | Optional. You can specify a value, then only the keys with this value are returned |
| strict | Optional. Used with the value parameter. Possible values:
|
Example 1
<?php
$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a));
?>
$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a));
?>
Array ( [0] => a [1] => b [2] => c )
Example 2
Using the value parameter.
<?php
$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a,"Dog"));
?>
$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a,"Dog"));
?>
Array ( [0] => c)
Example 3
Using the strict parameter: false
<?php
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",false));
?>
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",false));
?>
Array ( [0] => 0 [1] => 3 )
Example 4
Using the strict parameter: true
<?php
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",true));
?>
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",true));
?>
OUTPUT :
Array ( [0] => 3)