How can one search for the key of an element in a multidimensional array in PHP?

When working with multidimensional arrays in PHP, it can sometimes be challenging to search for the key of a specific element. One way to do this is by using a combination of array functions such as array_search and array_keys. By iterating through the array and checking each element, you can find the key associated with the desired value.

<?php
// Sample multidimensional array
$multiArray = array(
    "fruit" => array("apple", "orange", "banana"),
    "vegetable" => array("carrot", "broccoli", "spinach")
);

// Function to search for the key of an element in a multidimensional array
function searchKeyInMultiArray($array, $value) {
    foreach ($array as $key => $subArray) {
        if (in_array($value, $subArray)) {
            return $key;
        }
    }
    return null;
}

// Search for the key of "banana" in the multidimensional array
$result = searchKeyInMultiArray($multiArray, "banana");
echo "Key for 'banana' is: " . $result;
?>