Are there best practices or guidelines for beginners in PHP to efficiently search for keys in multidimensional arrays using text strings?

When searching for keys in multidimensional arrays using text strings in PHP, it's important to iterate through the array recursively to check for matching keys. One efficient way to do this is by creating a function that takes the array and the search string as parameters, and then recursively searches through the array for keys that match the search string.

function searchArrayKeys($array, $searchKey) {
    $results = [];
    
    foreach ($array as $key => $value) {
        if ($key === $searchKey) {
            $results[] = $value;
        }
        
        if (is_array($value)) {
            $results = array_merge($results, searchArrayKeys($value, $searchKey));
        }
    }
    
    return $results;
}

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

$searchKey = 'subkey1';
$results = searchArrayKeys($array, $searchKey);

print_r($results);