PHP Array key() Function

What is key() Function?

Explanation

The "key()" function can fetch a key from an array and the array pointer is moved using the next() function.

Syntax:


key(array)

In the above syntax "array" is the array from which a key has to be fetch.

Example :


<?php
$a = array
(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
while ($fruit_name = current($a))
{
if ($fruit_name == 'apple') {
echo key($a).'<br />';
}
next($a);
}
?>
Result :

fruit1
fruit4
fruit5

In the above example, for every value "Apple" the keys are displayed using the loop.

PHP Topics


Ask Questions

Ask Question