dlondero
dlondero

Reputation: 2595

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

I'd like to recursively update the array below replacing the content of arrays containing a [myKey] key with some other values (let's say [foo] => bar, [bar] => foo). This without using references as I've already some code working but I want to refactor it.

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
                )
        )
)

What I'd like to have in the end is below. Don't think at how I'll decide which key/values to use, just how to add them to the arrays...

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
                )
        )
)

Thanks in advance!

Upvotes: 0

Views: 1209

Answers (1)

Pieter
Pieter

Reputation: 1833

You can try this function:

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;   

}

Upvotes: 3

Related Questions