php

PHP array_keys() Function

The array_keys() is used if you want to return the array keys from an array in PHP. It takes three parameters. By default, it will return all the keys present in the input array.

Syntax:
array_keys(array_input,value,strict)

Parameters:

  1. array_input is the array.
  2. The value is the second parameter (optional) used to return keys based on the value.
  3. Strict is an optional parameter used with a value parameter, that specifies the datatype of values. It takes boolean values. If it is true, then it will consider the different data type values as different, if it is false, it considers the different data types as the same.

Let’s see examples to understand this concept better.

Example 1: array_keys() with only array_input

In this example, we will create an array: Flower that has 4 key-value pairs. ‘flower_name1’=>’lotus’,’flower_name2’=>’lilly’,’flower_name3’=>’lotus’,’flower_name4’=>’rose’

After that, we will return all keys present in the array.

<?php

//create an array named Flower1 with 4 key-values.
$Flower=array('flower_name1'=>'lotus', 'flower_name2'=>'lilly', 'flower_name3'=>'lotus','flower_name4'=>'rose');

print_r($Flower);
echo "All keys: ";
print_r(array_keys($Flower));

?>

Output:

We can see that all keys were returned.

Example 2: array_keys() with array_input and value as parameters

In this example, we will create an array: Flower that has 4 key-value pairs. ‘flower_name1’=>’lotus’,’flower_name2’=>’lilly’, ‘flower_name3’=>’lotus’,’flower_name4’=>’rose’

After that, we will return keys present in the array with the value “lotus”.

<?php

//create an array named Flower1 with 4 key-values.
$Flower=array('flower_name1'=>'lotus', 'flower_name2'=>'lilly', 'flower_name3'=>'lotus', 'flower_name4'=>'rose');

print_r($Flower);
echo "All keys with lotus: ";
print_r(array_keys($Flower,"lotus"));

?>

Output:

We can see that the lotus associated with the keys – flower_name1 and flower_name3 were returned.

Example 3: array_keys() with array_input,value and strict

In this example, we will create an array: Flower that has 4 key-value pairs. ‘flower_size1’=>’2′,’flower_size2’=>’3’,’flower_size3’=>3,’flower_size4’=>2.

After that, we will return keys present in the array with the value of 2 by specifying the strict parameter as true.

<?php

//create an array named Flower1 with 4 key-values.
$Flower=array('flower_size1'=>'2', 'flower_size2'=>'3', 'flower_size3'=>3, 'flower_size4'=>2);

print_r($Flower);
echo "All keys with 2: ";
print_r(array_keys($Flower,2,true));

?>

Output:

We can see that the 2 associated with the key flower_size4 is returned. But value -2 occurs two times. As we specified strict as true, it considers only specified data types, i.e., 2 (integer) and considers it as only one.

Example 3:array_keys() with array_input,value and strict

In this example, we will create an array: Flower that has 4 key-value pairs. ‘flower_size1’=>’2′,’flower_size2’=>’3’,’flower_size3’=>3,’flower_size4’=>2.

After that, we will return the keys present in the array with value – 2 by specifying the strict parameter as false.

<?php

//create an array named Flower1 with 4 key-values.
$Flower=array('flower_size1'=>'2', 'flower_size2'=>'3', 'flower_size3'=>3, 'flower_size4'=>2);

print_r($Flower);
echo "All keys with 2: ";
print_r(array_keys($Flower,2,false));

?>

Output:

We can see that the 2 associated with the key flower_size1 and flower_size4 are returned. As we specified strict as false, it considers all the data types, i.e., integer and string.

Conclusion

In this article, we saw how to return all the keys from an array using the array_keys() function. It is possible to get keys to a particular value by specifying the value. Strict is an optional parameter used with a value parameter that specifies the datatype of values. It takes boolean values. If it is true, then it will consider the different data types as different, if it is false, it considers the different data type as the same.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain