2

Is there any predefined PHP function to find a key in a multi dimensional array?

In the below example - there is a variable name 'rose' and I need to get the key of the array by using the variable name. The result of the key is "flower".

$array = array (
                    'fruits' => array (
                                            'mango',
                                            'cherry'
                                    ),
                    'flowers' => array (
                                            'rose'
                                    )
            );

How do I achieve this?

Raj Sf
  • 1,408
  • 1
  • 17
  • 26

1 Answers1

4

Loop it up using a foreach

$keyword='mango';
foreach($array as $k=>$arr)
{
    if(in_array($keyword,$arr))
    {
        echo $k;break;// "prints" fruits
    }
}

Working Demo

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126