PHP array_keys Function | Return Array Containing Keys

PHP array_keys is an inbuilt Function in PHP which returns either all the keys or a subset of keys of an array. It takes an array in input and returns an array containing the keys of the array. In this article, we will discuss the PHP array_keys Function. Also, we will discuss a few examples of using it.

Syntax

array array_keys($inputArray, $searchValue = null, $strict = false)

Parameters

The PHP array_keys Function accepts three parameters. However, only one parameter is mandatory and the other two are optional. The parameters are described below:

  • $inputArray: The first parameter is the array from which you need to return the keys. This parameter is mandatory.
  • $searchValue: The second parameter is optional. If you pass the search value then the function returns only the keys containing this value. Otherwise, it returns all the keys of the array.
  • $strict: This parameter defines if the strict search (===) will be used or not. If the parameter is true, then the function matches value and data type of the value. However, by default the value of the strict parameter is false. It is also an optional parameter.

Return Value

The PHP array_keys Function returns an array containing either all or a subset of keys in an array. If the searchValue parameter is passed, then only the keys with that value are returned. Otherwise, the function returns all the keys of the array.

array_keys only return the first level of keys. If you pass a multi-dimensional array then it returns the keys only on the first level.

PHP array_keys Function Example
PHP array_keys Function Example

Examples

Let’s discuss a few examples for using PHP array_keys Function.

Example 1: Basic Usage

<?php

	$testArray = array('a' => 'apple', 'b' => 'ball', 10 => 'cat', 'd' =>'dog', 'e' => array('f' => 'fish'));
	$keys = array_keys($testArray);
	print_r($keys);
	/*
	Array
	(
	    [0] => a
	    [1] => b
	    [2] => 10
	    [3] => d
	    [4] => e
	)
	*/
?>

For instance, consider a multi-dimensional array containing mixed keys. You can observe that PHP array_keys Function returns an array containing all the keys on the first level. Notice, it doesn’t return the keys in the nested array. In order to make that possible, you can call the function like below:

<?php       
        print_r(array_keys($testArray['e']));
	/*
	Array
	(
	    [0] => f
	)	
	*/
?>

Example 2: Passing Search Value

For instance, consider an example with passing search value.

<?php

	$testArray = array('apple', 'mango', 'apple', 'banana', 'grapes', 'orange', 'apple');
	$keys = array_keys($testArray, 'apple');
	print_r($keys);
	/*
	Array
	(
	    [0] => 0
	    [1] => 2
	    [2] => 6
	)
	*/
?>

In the above example, we pass ‘apple’ as the search value. The function returns an array containing those keys whose value matches the search value.

Example 3: Strict Value

<?php

	$testArray = array('a' => '1', 'b' => 1, 'c' => '2');
	print_r(array_keys($testArray, 1));
	/*
	Array
	(
	    [0] => a
	    [1] => b
	)
	*/

	print_r(array_keys($testArray, 1, true));
	/*
	Array
	(
	    [0] => b
	)
	*/

?>

In the above example, notice the difference with passing true in the strict value parameter. The array contains two occurrences of 1. One is string and the other is integer. We search for an integer 1 using array_keys Function. When the strict value parameter is not passed (default false), the function returns both the keys with 1. However, when the value of strict parameter is true, the function matches the data type along with the values and returns only one key.

Conclusion

In this article, we discussed the array_keys Function in PHP. You can read more about it on the Official Documentation of PHP. Also, you can read about other Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *