PHP array_keys() Function
In this article, we’ll explore PHP array_keys() function and how it can be used in your PHP code. If you’re a PHP programmer, you’ve likely used arrays at some point. Arrays are an essential part of programming in PHP, as they allow you to store and manipulate multiple values in a single variable.
One function that you may find useful when working with arrays is the array_keys() function.
What is array_keys() Function?
PHP array_keys() is a built-in function that returns an array containing the keys of an array. In other words, it returns an array of all the keys in the array that you pass to it. There is only one parameter needed to call this function, and that is the array whose keys need to be retrieved.
It is possible to use this function in situations where you need to obtain all the keys in your array. This is when you want to iterate over an array of associative keys and apply some action to each key in your array; this is the most efficient function to perform.
Syntax
array_keys(array, value, strict)
Parameter Values
Parameters | Overview |
array | This is required. This method specifies an array of elements |
value | It is optional. Specifying a value allows you to limit the return of keys to those that contain this value |
strict | It is optional. This parameter is used in conjunction with the value parameter. The following are possible values:
|
Using the array_keys() function
To use the Func array keys / array_keys() function, you first need to have an array that you want to get the keys from.
A list of keys is returned in the form of an array:
Example: 
Case-sensitive Searches
By default, the Func array keys / array_keys() function performs a case-insensitive search. This means that if you search for a value that is capitalized differently than the values in the array, the function will still find it.
Searching for a Specific Value
If you want to get the keys of only the elements in an array that have a specific value, you can pass that value as the second parameter to the Func array keys OR array_keys() function.
The value parameter can be used as follows:
Example: 
Example: 
Example: 
Example Explanation:
In this example, we are using PHP to create an array called $array1, which contains a mix of numbers and a string: 2, 6, 8, 9, “45”, 8, and 4.
We then use the array_keys function to search for the value “45” within $array1, and return an array containing the keys (indexes) of any matching elements. The second argument of array_keys, true, indicates that we want an exact match.
Finally, we use the print_r function to output the resulting array of keys.
Understanding Undefined Array Keys
As you work with PHP arrays, you might encounter situations where you try to access an element using a specific key, but that key doesn’t exist. This results in a pesky “Undefined array key” warning, potentially causing your code to break. But fret not! With a few smart techniques, you can gracefully handle these scenarios without breaking a sweat.
Example 1: Checking for Key Existence
One of the simplest ways to avoid the “Undefined array key” warning is to check if the key exists before accessing it. We can do this using the isset() function.
Example: 
Example Explanation:
In this example, we have an array containing information about celebrities. Before accessing the value of the ‘Brad Pitt’ key, we check if it exists using isset()
. If the key is found, we display the celebrity’s profession; otherwise, we inform the user that the information is not available.
Example 2: The Ternary Operator
PHP offers a concise way to handle this scenario using the ternary operator. Let’s see how it works:
Example: 
Example Explanation:
Here, we have an array with fruit colors. Instead of writing an if-else block, we utilize the ternary operator to check for key existence. If the key exists, we assign the corresponding color to the $color
variable; otherwise, we set it to “unknown.”
Conclusion
In conclusion, PHP array_keys() is a useful tool to have in your PHP arsenal when working with arrays. It allows you to quickly get an array of all the keys in an array, or only the keys of elements with a specific value. It also allows you to perform case-insensitive or case-sensitive searches, depending on your needs. With this knowledge, you should be able to use the array_keys() function effectively in your PHP code.
Please subscribe to our newsletter below in order to stay up to date with all the latest developments in PHP Array Function and to learn more about the language.