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!

4
  • Does this answer your question? 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.) Commented Feb 3, 2021 at 13:57
  • Hm no, that's all about getting the value. I wish to set it. Commented Feb 3, 2021 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.) Commented Feb 3, 2021 at 14:16
  • How to access and manipulate multi-dimensional array by key names / path? Commented Feb 3, 2021 at 14:16

1 Answer 1

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
            )

    )
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply, unfortunaly I don't want to echo it. I want to set it in the $arr.
You should account for the fact that the path might be incorrect (to avoid undefined index notices).
Yeah that is what I need. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.