Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

PHP key() function (with example)

Hi everyone,

I have been working with PHP and came across the "key()" function. I have read the official documentation, but I'm still a bit confused about how to use it properly. Could someone please provide me with a clear explanation of the "key()" function and maybe show me an example of how it can be used?

Thank you in advance for your help!

All Replies


Hey there,

I'd like to contribute to this discussion by sharing my personal experience with the "key()" function in PHP. When it comes to working with arrays, the "key()" function plays a crucial role in obtaining the current key of an array element during traversal.

In one of my recent projects, I had to iterate through a multidimensional array to perform certain operations based on specific keys. The "key()" function proved to be very handy in this scenario. It allowed me to access the current key within the loop and make decisions based on that information.

Here's an example to give you a clearer picture:

$books = array(
"fiction" => array("Harry Potter", "To Kill a Mockingbird", "1984"),
"non-fiction" => array("Sapiens", "Educated", "Becoming")

foreach ($books as $genre => $titles) {
echo "Genre: " . $genre . "<br>";

foreach ($titles as $key => $title) {
echo "Key: " . $key . ", Title: " . $title . "<br>";

echo "<br>";

In this example, I have a multidimensional array called `$books`, where the keys represent different genres and the values are arrays of book titles. By using the `key()` function within nested loops, I was able to access the current key for each book title.

The output of this code snippet would be:

Genre: fiction
Key: 0, Title: Harry Potter
Key: 1, Title: To Kill a Mockingbird
Key: 2, Title: 1984

Genre: non-fiction
Key: 0, Title: Sapiens
Key: 1, Title: Educated
Key: 2, Title: Becoming

By incorporating the "key()" function, I was able to gather information about the current keys within both the outer and inner loops. This allowed me to handle the array elements with more precision and tailor my operations accordingly.

I hope this personal anecdote sheds some light on the practical usage of the "key()" function. If you have any further questions, feel free to ask!


Hi there,

I'd be happy to share my personal experience with the "key()" function in PHP. The "key()" function is used to retrieve the key of the element that is currently being pointed to by the internal pointer of an array. In other words, it returns the key of the element that is currently being processed within a loop.

Let me provide you with an example to better illustrate its usage:

$fruits = array("apple" => "red", "banana" => "yellow", "grape" => "purple");

// Resetting the internal pointer to the first element

// Using a while loop to iterate through the array
while ($key = key($fruits)) {
echo "Key: " . $key . ", Value: " . $fruits[$key] . "<br>";

// Moving the internal pointer to the next element

In this example, we have an array called `$fruits` with three elements. We use the `key()` function to retrieve the key of the current element within the loop. We then print both the key and the corresponding value. By calling `next()` at the end of each iteration, we move the internal pointer to the next element in the array.

The output of the above code will be:

Key: apple, Value: red
Key: banana, Value: yellow
Key: grape, Value: purple

As you can see, the `key()` function allows us to access the keys of the array elements while iterating over them. This can be useful when you need to perform specific operations based on the keys of the array elements.

I hope this explanation helps! Let me know if you have any further questions.

New to LearnPHP.org Community?

Join the community