1function findKey($array, $keySearch)
2{
3 foreach ($array as $key => $item) {
4 if ($key == $keySearch) {
5 echo 'yes, it exists';
6 return true;
7 } elseif (is_array($item) && findKey($item, $keySearch)) {
8 return true;
9 }
10 }
11 return false;
12}
13
1
2<?php
3$array = array(
4 'fruit1' => 'apple',
5 'fruit2' => 'orange',
6 'fruit3' => 'grape',
7 'fruit4' => 'apple',
8 'fruit5' => 'apple');
9
10// this cycle echoes all associative array
11// key where value equals "apple"
12while ($fruit_name = current($array)) {
13 if ($fruit_name == 'apple') {
14 echo key($array).'<br />';
15 }
16 next($array);
17}
18?>
19
20
1<?php
2$array = array(
3 'fruit1' => 'apple',
4 'fruit2' => 'orange',
5 'fruit3' => 'grape',
6 'fruit4' => 'apple',
7 'fruit5' => 'apple');
8
9// this cycle echoes all associative array
10// key where value equals "apple"
11while ($fruit_name = current($array)) {
12 if ($fruit_name == 'apple') {
13 echo key($array).'<br />';
14 }
15 next($array);
16}
17?>