问题
this relates to my previous post. how to create a collection of multi dimensional arrays and not overwrite origional values when new ones are pushed
i am thinking my problem has to do with how i am creating the array. what i'm trying to do is make an array that looks like this
Array
(
[10] => Array
(
[0] => 29
[1] => 36
)
)
into something like this
Array
(
[10] => Array
(
[0] => 29
[1] => 36
)
[20] => Array
(
[0] => 29
[1] => 36
)
[25] => Array
(
[0] => 29
[1] => 36
)
)
the 10, 20, and 25 is the product id where the numbers within those are the selections that were selected on that page (in the link i gave above). so each product would have its own collection selected.
when i use array_push instead of doing what i want it to do the first collection of array as in the first example keep reseting. so if i do my selections on say flyers and add to cart then i go to business cards and do my selections and add to cart the array resets and it becomes like the first example. whatever i try i cant get it to merge below a collection like the second example that i have. i have tried array_merge(),array_push but those dont really work.
回答1:
Solution:-
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:
$a = array(10 => array(25,26));
$b = array(22 => array(45,66));
$c = $a + $b;
print_r($c);
Output:-
Array
(
[10] => Array
(
[0] => 25
[1] => 26
)
[22] => Array
(
[0] => 45
[1] => 66
)
)
Hope this helps.
来源:https://stackoverflow.com/questions/5212924/merging-a-multi-dimensional-array-into-another-multi-dimensional-array