0

I'm trying to get reach a point in a dynamicly generated multidimensional array based on a array with keys.

Basicly I have the following array:

$arr = [
    "something" => [
        'something_else' => [
            "another_thing" => "boo"
        ]
    ],
    "something2" => [
        'something_elseghf' => [
            "another_thingfg" => [
                "hi" => "bye"
            ]
        ]
    ],
    "info" => [
        'something_else2' => [
            "another_thingh" => "boo"
        ]
    ],
];

Now I want to set a value in the array based on the keys in a different array:

$keyArr = ["something2", 'something_elseghf' "another_thingfg", "hi"];

So the above array means that I need to set the hi key to some value. How can I reach that part of the array with these random keys, note that the length of $keyArr is dynamic aswell. So I can't reach it with:

$arr[$keyArr[0]][$keyArr[1]][$keyArr[2]][$keyArr[3]] = 

Hope anyone has an idea on how to solve this!

Merijndk
  • 1,674
  • 3
  • 18
  • 35
  • Does this answer your question? [use strings to access (potentially large) multidimensional arrays](https://stackoverflow.com/questions/7003559/use-strings-to-access-potentially-large-multidimensional-arrays) (not exactly the same, but quite similar, so the solutions should be adaptable to your use case.) – misorude Feb 03 '21 at 13:57
  • Hm no, that's all about getting the value. I wish to set it. – Merijndk Feb 03 '21 at 13:58
  • Well, the solutions presented there us `return $vars;`, after $vars has been made to point to the correct location. So `$vars = $newValue;` should have the opposite effect. (It might be that you’d need to work with references though, so that this assignment actually “writes through” to the original element in the array.) – misorude Feb 03 '21 at 14:16
  • [How to access and manipulate multi-dimensional array by key names / path?](https://stackoverflow.com/questions/27929875/how-to-access-and-manipulate-multi-dimensional-array-by-key-names-path) – misorude Feb 03 '21 at 14:16

1 Answers1

1

Try this approach:

$arr = [
    "something" => [
        'something_else' => [
            "another_thing" => "boo"
        ]
    ],
    "something2" => [
        'something_elseghf' => [
            "another_thingfg" => [
                "hi" => "bye"
            ]
        ]
    ],
    "info" => [
        'something_else2' => [
            "another_thingh" => "boo"
        ]
    ],
];

$keyArr = ["something2", 'something_elseghf', "another_thingfg", "hi"];


$cursor = $arr;
foreach ($keyArr as $key) {
    $cursor = $cursor[$key];
}

echo $cursor;

Will echo

bye

UPDATE:

If you want to change a value within multi-dimentional array, then use a recursive function, like this:

function changeValue($array, $path, $value) {
    if (empty($path)) {
        return $value;
    }
    $key = array_shift($path);
    $array[$key] = changeValue($array[$key], $path, $value);
    return $array;
}

$arr = [
    "something" => [
        'something_else' => [
            "another_thing" => "boo"
        ]
    ],
    "something2" => [
        'something_elseghf' => [
            "another_thingfg" => [
                "hi" => "bye"
            ]
        ]
    ],
    "info" => [
        'something_else2' => [
            "another_thingh" => "boo"
        ]
    ],
];

$keyArr = ["something2", 'something_elseghf', "another_thingfg", "hi"];

$changedArray = changeValue($arr, $keyArr, 'New value!');

print_r($changedArray);

Will output

Array
(
    [something] => Array
        (
            [something_else] => Array
                (
                    [another_thing] => boo
                )

        )

    [something2] => Array
        (
            [something_elseghf] => Array
                (
                    [another_thingfg] => Array
                        (
                            [hi] => New value!
                        )

                )

        )

    [info] => Array
        (
            [something_else2] => Array
   

         (
                [another_thingh] => boo
            )

    )
)
nikserg
  • 382
  • 1
  • 11