合并的多维阵列到另一个多维数组 [英] merging a multi-dimensional array into another multi-dimensional array
问题描述
这涉及到我的previous职位。
<一href=\"http://stackoverflow.com/questions/5209265/how-to-create-a-collection-of-multi-dimensional-arrays-and-not-overwrite-origiona\">how创建多维数组的集合,当新推不覆盖原始值。
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
)
)
弄成这个样子
Array
(
[10] => Array
(
[0] => 29
[1] => 36
)
[20] => Array
(
[0] => 29
[1] => 36
)
[25] => Array
(
[0] => 29
[1] => 36
)
)
的
10,20和25是在产品id其中内的那些数字是被选择在该网页上(在我上面给出的链接)的选择。所以每个产品都将会有自己的收藏选择。
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.
当我使用array_push,而不是做什么,我想要它做数组的第一个集合作为第一个例子保持CH375复位。所以,如果我做的传单说我的选择,并添加到购物车,然后我去的名片,做我的选择,并添加到购物车阵复位,它变得像第一个例子。无论我尝试,我不能把它合并一样,我有第二个例子集合下方。我曾尝试array_merge(),array_push但那些真的不工作。
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.
推荐答案
解决方案 -
如果您想从第二个数组的数组元素追加到第一个数组,而不是覆盖从第一个数组中的元素,而不是重新索引,使用 +阵列联合运营商:
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);
输出 -
Array
(
[10] => Array
(
[0] => 25
[1] => 26
)
[22] => Array
(
[0] => 45
[1] => 66
)
)
希望这有助于。
这篇关于合并的多维阵列到另一个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!