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

ParametersOverview
arrayThis is required. This method specifies an array of elements
valueIt is optional. Specifying a value allows you to limit the return of keys to those that contain this value
strictIt is optional. This parameter is used in conjunction with the value parameter. The following are possible values:
  • true – Returns all keys in the list that have the specified value, depending on the type of key: the number 5 is not the same as the string “5 ”.
  • false – Default value. No matter what type of number you are looking at, the string “5” and the number 5 are both the same.

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: 

<?php $array1 = array("Orange" => 100, "Grapes" => 150, "Banana" => 250, "Apricot" => 350); print_r(array_keys($array1)); ?>

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: 

<?php $array1 = array("Orange" => 100, "Grapes" => 150, "Banana" => 250, "Apricot" => 350); print_r(array_keys($array1,250)); ?>
If the strict parameter, false, is used:

Example: 

<?php $array1 = array(2,45,8,9,"45",8,4); print_r(array_keys($array1, "45", false)); ?>
If the strict parameter, true, is used:

Example: 

<?php $array1 = array(2,6,8,9,"45",8,4); print_r(array_keys($array1, "45", true)); ?>

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: 

<?php $celebrities = array( "Tom Hanks" => "Actor", "Taylor Swift" => "Singer", "Bill Gates" => "Entrepreneur" );if (isset($celebrities['Brad Pitt'])) { echo "Brad Pitt's profession is: " . $celebrities['Brad Pitt']; } else { echo "Sorry, we couldn't find information about Brad Pitt."; } ?>

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: 

<?php $fruit_colors = array( "apple" => "red", "banana" => "yellow", "orange" => "orange" );$fruit = "kiwi"; $color = isset($fruit_colors[$fruit]) ? $fruit_colors[$fruit] : "unknown"; echo "The color of the $fruit is: " . $color; ?>

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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon