Home >>PHP Array Functions >PHP array_keys() Function

PHP array_keys() Function

PHP array_keys() Function

PHP array_keys() function is used to return either all the keys of the given input array or the subset of the keys. It accepts three parameters out of which one is mandatory and other two are optional. It returns an array as output containing either all of the keys or subset of keys of the input array.

Syntax:

array_keys($array, $value, $strict);

Parameter Values

Parameter Description
array This is a required parameter. This parameter defines an array.
value This is an optional parameter. This parameter defines a value that only the keys with this value are returned.
strict This is an optional parameter. This parameter is used with the value parameter.

Here is an example of array_keys() function in PHP:

<html>
<body>
<pre>
<?php
$a=array("1"=>"a","2"=>"b","a"=>"x","3"=>"f","Q"=>"z","5"=>"k","g"=>"9");
print_r(array_keys($a));
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => a
    [3] => 3
    [4] => Q
    [5] => 5
    [6] => g
)

Example 2:

<html>
<body>
<pre>
<?php
$a=array("1"=>"a","2"=>"b","a"=>"x","3"=>"a","Q"=>"z","5"=>"a","g"=>"a");
print_r(array_keys($a,"a"));
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => 1
    [1] => 3
    [2] => 5
    [3] => g
)

No Sidebar ads