在多维数组中递归替换特定键的元素(每次替换仅一次)


Recursively replace elements for specific key (only once per replacement) in a multidimensional array

我想递归地更新下面的数组,将包含[myKey]键的数组的内容替换为其他值(比如[foo]=>bar,[bar]=>foo)。这不需要使用引用,因为我已经有一些代码在工作,但我想重构它

Array
(
    [objects] => Array
        (
            [0] => Array
                (
                    [somekey] => value
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [myKey] => item1
                                )
                            [1] => Array
                                (
                                    [myKey] => item2
                                )
                        )
                )
            [1] => Array
                (
                    [otherKey] => other value
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [myKey] => item3
                                )
                            [1] => Array
                                (
                                    [myKey] => item4
                                )
                        )
                )
            [2] => Array
                (
                    [myKey] => item5
                )
        )
)

我最后想要的东西在下面。不要考虑我将如何决定使用哪些键/值,只考虑如何将它们添加到数组中。。。

Array
(
    [objects] => Array
        (
            [0] => Array
                (
                    [somekey] => value
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [foo] => bar
                                )
                            [1] => Array
                                (
                                    [bar] => foo
                                )
                        )
                )
            [1] => Array
                (
                    [otherKey] => other value
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [whatever] => value
                                )
                            [1] => Array
                                (
                                    [foo1] => bar
                                )
                        )
                )
            [2] => Array
                (
                    [bar1] => foo2
                )
        )
)

提前感谢!

您可以尝试此功能:

function replace_recursive_array($array, $old_key, $new_key, $new_value) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $array[$key] = replace_recursive_array($value, $old_key, $new_key, $new_value);
        }elseif($key == $old_key) {
            $array[$new_key] = $new_value;
        }
    }
    unset($array[$old_key]);          
    return $array;   
}