How can the use of a custom function, like array_values_recursive(), simplify the process of extracting specific values from a multidimensional array in PHP?

When working with multidimensional arrays in PHP, extracting specific values can be cumbersome and require nested loops. By using a custom function like array_values_recursive(), the process can be simplified by recursively flattening the array and returning only the values. This allows for easier access to specific values without the need for complex nested loops.

function array_values_recursive($array) {
    $values = [];
    
    foreach($array as $value) {
        if(is_array($value)) {
            $values = array_merge($values, array_values_recursive($value));
        } else {
            $values[] = $value;
        }
    }
    
    return $values;
}

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

$values = array_values_recursive($multidimensionalArray);
print_r($values);