|
|
Tutorials » Php »
|
Topic |
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.
|
|
A Note |
Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial.
Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.
|
|
|
|