What are the limitations of using array_search in PHP when dealing with nested arrays?

When dealing with nested arrays, array_search in PHP can only search for values in the top-level of the array and does not support searching through nested arrays. To search for a value within nested arrays, you can create a custom recursive function that traverses through all levels of the array.

function recursive_array_search($needle, $haystack) {
    foreach($haystack as $key => $value) {
        if($value === $needle) {
            return $key;
        } elseif(is_array($value)) {
            $result = recursive_array_search($needle, $value);
            if($result !== false) {
                return $result;
            }
        }
    }
    return false;
}

$nestedArray = [
    'key1' => 'value1',
    'key2' => [
        'key3' => 'value2',
        'key4' => 'value3'
    ],
    'key5' => 'value4'
];

$searchValue = 'value3';
$key = recursive_array_search($searchValue, $nestedArray);

if($key !== false) {
    echo "Key found: " . $key;
} else {
    echo "Value not found";
}