# Referring to nested arrays and array_merge (php)

In such an array:

$arr = Array ( [key0] => Array ( [subkey0] => Array (  => "value0"  => "value1" ) [subkey1] => Array (  => "value0"  => "value1" ) ) [key1] => Array ( [subkey0] => Array (  => "value0"  => "value1" ) [subkey1] => Array (  => "value0"  => "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" => array_merge((array)$arr[$key]["subkey0"], (array)$var1), "subkey1" => 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" =>+ (array)$varX  Or use array_merge() in a different way to add $varX to the end of subkeyX as [X] => $varX. ## 1 Answer 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;
}

• Thanks but it does not seem to work. For instance, check this phpfiddle -- click on run to see the results. – Alex Mar 30 '13 at 1:11
• Sorry, my answer was not clear enough. I edited it. – AlucardTheRipper Mar 30 '13 at 1:54
{"ft_lang_label":"__label__en","ft_lang_prob":0.7734696,"math_prob":0.917848,"size":845,"snap":"2021-21-2021-25","text_gpt3_token_len":302,"char_repetition_ratio":0.20570749,"word_repetition_ratio":0.33783785,"special_character_ratio":0.42366865,"punctuation_ratio":0.10489511,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9960643,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-17T01:05:23Z\",\"WARC-Record-ID\":\"<urn:uuid:44e41b9b-c533-42f7-871b-94124709981d>\",\"Content-Length\":\"167604\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9eb52db9-8d10-4c17-83ff-3745f5a6de4a>\",\"WARC-Concurrent-To\":\"<urn:uuid:2c09cf6b-5381-4ea6-8851-3e288af07a88>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/24522/referring-to-nested-arrays-and-array-merge-php\",\"WARC-Payload-Digest\":\"sha1:EUQCYWNOFGNQCAH7P24JTHJP4RXQP3OE\",\"WARC-Block-Digest\":\"sha1:B2AHWY4S6KETE2X5FXKFP6LRZBUZWLE5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243991921.61_warc_CC-MAIN-20210516232554-20210517022554-00239.warc.gz\"}"}