Referring to nested arrays and array_merge (php) In such an array:

$arr = Array (
    [key0] =&gt; Array (
        [subkey0] =&gt; Array (
            [0] =&gt; "value0"
            [1] =&gt; "value1"
        )
        [subkey1] =&gt; Array (
            [0] =&gt; "value0"
            [1] =&gt; "value1"
        )
    )
    [key1] =&gt; Array (
        [subkey0] =&gt; Array (
            [0] =&gt; "value0"
            [1] =&gt; "value1"
        )
        [subkey1] =&gt; Array (
            [0] =&gt; "value0"
            [1] =&gt; "value1"
        )
    )
)


In order to add $varX to the end of $arr[keyX][subkeyX] as [X] => $varX, I use, inside a loop, the following:

$arr[$key] = array(
    "subkey0" =&gt; array_merge((array)$arr[$key]["subkey0"], (array)$var1),
    "subkey1" =&gt; array_merge((array)$arr[$key]["subkey1"], (array)$var2)
    ...
)


And I might be working with even more nested arrays, so I was wondering if there was a way for subkey0 to refer to itself, or if I could do something like:

"subkeyX" =&gt;+ (array)$varX


Or use array_merge() in a different way to add $varX to the end of subkeyX as [X] =&gt; $varX.
I don't know if I understand the question right. But this should work.

foreach ($keys as $key) {
    $arr2[$key]['subkey0'][] = $var1;
    $arr2[$key]['subkey1'][] = $var2;
}