Monday 3 September 2018

Push array php - associative arrays and keep associative keys

I have several associative arrays, each starting with a string key. I also have a master array that i want to use to combine each of these sub arrays. When using array_push though, each array is then given an additional numeric key in the master array.

How can i avoid this and push the sub arrays into the master array keeping the keys intact?

$master_array = array_merge($master_array, $sub_array_1, $sub_array_2, ...) ;

Beware of what happens when the sub arrays have the same keys - if they are numeric, you will get both values, but if not, later values will over-write earlier ones.

0 comments:

Post a Comment