What are common challenges when searching for a key in a multidimensional array using a text string in PHP?

When searching for a key in a multidimensional array using a text string in PHP, one common challenge is that you need to iterate through the array recursively to find the key. You can create a recursive function that checks each element of the array and its subarrays until the key is found.

function searchKeyInMultidimensionalArray($array, $key) {
    foreach ($array as $k => $value) {
        if ($k === $key) {
            return $value;
        } elseif (is_array($value)) {
            $result = searchKeyInMultidimensionalArray($value, $key);
            if ($result !== null) {
                return $result;
            }
        }
    }
    return null;
}

// Example usage
$multidimensionalArray = [
    'key1' => 'value1',
    'key2' => [
        'subkey1' => 'subvalue1',
        'subkey2' => 'subvalue2'
    ]
];

$keyToSearch = 'subkey1';
$result = searchKeyInMultidimensionalArray($multidimensionalArray, $keyToSearch);
echo $result; // Output: subvalue1