PHP array_keys() Function

What is PHP array_keys() Function?

If you want to get all the keys of an array or keys of a element of that array, use array_keys() function. It can handle both indexed and associative arrays. So the function takes only one array as input and returns an indexed array with the keys of the input array as values. This function saves you time and codes to extract keys of an array in compare to manually extracting through loop.

Syntax:

array_keys(array, filter_value, strict)

Parameters:

The Function has 1 required parameter and 2 optional parameters-

array (Required): The array from which keys are to be extracted. Check example 1.

filter_value (Optional): The value for which keys are to be extracted. Check example 2.strict (Optional): The Boolean value that determines whether strict comparison (===) should be applied during the search with the second parameter filter_value.

  • FALSE – If it’s value is set to FALSE, the function returns all the keys whose values are same as the second parameter regardless of their type. So, in this case. The function performs loose comparison (==). It’s the default value. So, if you don’t mention the strict parameter, the function considers it as FALSE. So, it considers “10” and 10 are same. Check example 3.
  • TRUE – If it’s value is set to TRUE, the function returns all the keys whose values and their types are same as the second parameter. So, in this case. The function performs strict comparison (===). So, it doesn’t consider “10” is same as 10. Check example 4.

Return Values:

The function returns all the keys or a selected set of keys depending on the 2 optional parameters. If the value doesn’t exist in the array, it returns an empty array. Check example 5.

Examples:

Example 1: Extracting keys with array_keys()-

<pre>
<?php
$array = [1, 24.16, 100];
print_r(array_keys($array));
echo "<br />";
$array = ["Script" => "PHP", "Database" => "MySQL", "CMS" => "Wordpress"];
print_r(array_keys($array));
echo "<br />";
$array = [1, 24.16, "Language" => "PHP", 100];
print_r(array_keys($array));
?>
</pre>

Output:

Array

(

    [0] => 0

    [1] => 1

    [2] => 2

)


Array
(
    [0] => Script
    [1] => Database
    [2] => CMS
)
Array
(
    [0] => 0
    [1] => 1
    [2] => Language
    [3] => 2
)

Explanation:

The first array_keys function retrieves all the keys of a numeric key array. The second one retrieves all the keys of a string key array. The third one retrieves all the keys of an array that consists of both string key and numeric key. Note, the numeric keys are counted serially without considering string keys between them. So, key for the value “100” is 2 not 3.

Example 2: Extracting all the keys of a value in an array –

<pre>
<?php // #2
$array = [1, 24.16, 100, 24.16];
print_r(array_keys($array, 24.16));
echo "<br />";
$array = ["Script" => "PHP", "Database" => "MySQL", "CMS" => "Wordpress", "Language" => "PHP"];
print_r(array_keys($array, "PHP"));
?>
</pre>

Output:

Array
(
    [0] => 1
    [1] => 3
)
Array
(
    [0] => Script
    [1] => Language
)

Explanation:

The first array_keys() function retrieves all the keys of a integer in a string key array. The second one retrieves all the keys of a string in a string key array.

Example 3: Extracting all the keys of a value regardless of its type in an array-

<pre>
<?php
$array = ["Script" => "PHP", "Version" => 0, "Language" => "HTML", "Function" => "0"];
print_r(array_keys($array, "0", FALSE));
echo "<br />";
$array = ["Script" => "PHP", "Version" => 0, "Language" => "HTML", "Function" => "0"];
print_r(array_keys($array, 0));
?>
</pre>

Output:

Array
(
    [0] => Version
    [1] => Function
)
Array
(
    [0] => Version
    [1] => Function
)

Explanation:

Line 4: As the third parameter is set to FALSE, the function considers all the “0”s that include string “0” or numeric 0.

Line 7: When the third parameter is not mentioned explicitly, the function considers it as FALSE. So, it acts same as line 4.

Example 4: Extracting all the keys of a value with same type in an array-

<pre>
<?php
$array = ["Script" => "PHP", "Version" => 0, "Language" => "HTML", "Function" => "0"];
print_r(array_keys($array, "0", TRUE));
echo "<br />";
$array = ["Script" => "PHP", "Version" => 0, "Language" => "HTML", "Function" => "0"];
print_r(array_keys($array, 0, TRUE));
?>
</pre>

Output:

Array
(
    [0] => Function
)
Array
(
    [0] => Version
)

Explanation:

Line 4: As the third parameter is set to TRUE, the function considers only the values that are “0” not 0.

Line 7: As the third parameter is set to TRUE, the function considers only the values that are 0 not “0”.

Example 5: Extracting all the keys of a value that doesn’t exist in an array-

<pre>
<?php
$array = [1, 24.16, 100];
print_r(array_keys($array, 80));
?>
</pre>

Output:

Array
(
)

Explanation:

As 80 is not in the array, the function returns an empty array.

Example 6: Extracting all the keys of the value “1” in an array that also includes Boolean values-

<pre>
<?php
$array = [1, 24.16, 1, TRUE];
print_r(array_keys($array, 1));
echo "<br />";
$array = [1, 24.16, 1, TRUE, 0, FALSE];
print_r(array_keys($array, 0));
?>
</pre>

Output:

Array
(
    [0] => 0
    [1] => 2
    [2] => 3
)
Array
(
    [0] => 4
    [1] => 5
)

Explanation:

Line 4: As TRUE is considered as 1, so the function also returns the key of TRUE.

Line 7: As FALSE is considered as 0, so the function also returns the key of FALSE.

Example 7: Extracting numeric key surrounded by quotations-

<pre>
<?php
$array = [1 => 1, 2 => 24.16, 3 => 1, "1" => 1];
var_dump(array_keys($array, 1));
?>
</pre>

Output:

array(2) {
  [0]=>
  int(1)
  [1]=>
  int(3)
}

Explanation:

Though, the key of the last element 1 is surrounded by quotes, the function considers its type as integer.

Example 8: Extracting all the keys of a value in a multidimensional array-

<pre>
<?php
$array = array(
    "Client" => array("HTML","CSS"),
    "Server" => array("PHP","MySQL"),
    "OS" => "Linux"
);
var_dump(array_keys($array));
echo "<br />";
var_dump(array_keys($array, "CSS"));
echo "<br />";
var_dump(array_keys($array, "Linux"));
echo "<br />";
var_dump(array_keys($array, array("HTML","CSS")));
?>
</pre>

Output:

array(3) {
  [0]=>
  string(6) "Client"
  [1]=>
  string(6) "Server"
  [2]=>
  string(2) "OS"
}
array(0) {
}
array(1) {
  [0]=>
  string(2) "OS"
}
array(1) {
  [0]=>
  string(6) "Client"
}

Explanation:

Line 8: The function only retrieves keys of the first level values not that of the nested array.

Line 10: As the function only retrieves keys from the first level values not that from the nested array, so, it can’t find “CSS” and returns an empty array.

Line 12: The function finds “Linux” the first level, so it returns its key. Line 14: The function considers array(“HTML”, “CSS”) as a value in the first level and it returns its key.

Cautions:

  • Boolean TRUE is considered is as 1 and FALSE is considered as 0. Among the other values, if there is any Boolean TRUE and you try to retrieve all the keys of the value 1, the function also returns index of Boolean TRUE. Same thing happens for Boolean FALSE. Check example 6.
  • Even numeric key is surrounded by quotations, it is treated as a numeric number. It can confuse you regarding the output. Check example 7.
  • array_keys() functions can’t retrieve keys if it is in the nested arrays in a multidimensional array. Instead of a value, you can retrieve the key of an array though. Check example 8.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP array_keys() Function

array_keys() helps to get all the keys or selected keys from an array. It is one of the most used built-in array functions in PHP. Understanding this function properly and with combination of other function, you can accomplish many tasks easily.

Reference:

https://www.php.net/manual/en/function.array-keys.php