如何创建多维数组的集合,当新推不覆盖原始值 [英] how to create a collection of multi dimensional arrays and not overwrite origional values when new ones are pushed

查看:134
本文介绍了如何创建多维数组的集合,当新推不覆盖原始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里是我的地盘
http://69.231.195.173:8888/iadprint/products?product=flyers

。这是执行code。

if(isset($_POST['btnAddToCart']) && isset($_GET['product']))
    {
        $product_id = $action->getProductID($_GET['product']);

        $attribute[$product_id] = array();


        foreach ($_POST as $field=>$hash) 
        {
            $hash = $security->clean_numeric($hash);

            if($field != "btnAddToCart" && $field != 'price' && !empty($hash))
            {
                array_push($attribute[$product_id], $hash);

            }
        } 

        $_SESSION['iadprint_cart'] = $attribute;
}

这被形成的阵列看起来像这样

the array that gets formed looks like this

Array
(
    [10] => Array
        (
            [0] => 30
            [1] => 36
        )

)

这是主阵列内的10指传单产品ID名称。里面的30和36是选择的ID。

inside that main array the 10 refers to flyer product id name. inside of that the 30 and 36 are the ids of the selections.

遇到的问题是即时消息,如果你选择的名片,然后进行选择,并添加到购物车instaed在喜欢的图片的格式是数组()为新产品内的一切推。该数据被覆盖。我使用array_push,它应该工作,但事实并非如此。我究竟做错了什么?

the problem im having is if you select business card and make your selections and add to cart instaed of pushing in a format like the picture that is everything inside the array() for the new product. the data gets overridden. I am using array_push and it should work but it is not. what am i doing wrong?

如果您需要更多的解释,让我知道

if you need more explanation let me know

感谢

没有人知道吗?即时消息没有得到响应。请帮助我,我真的被卡住了卡住现在天我。

does anyone know? im not getting responds. please help me i am really stuck been stuck for days now.

推荐答案

解决方案 -

如果您想从第二个数组的数组元素追加到第一个数组,而不是覆盖从第一个数组中的元素,而不是重新索引,使用 +阵列联合运营商

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
        )
)

的关键是在这种情况下pserved $ P $。

The keys are preserved in this case.

从另一个问题同样的答案
  您发布关于这个问题: -
  <一href=\"http://stackoverflow.com/questions/5212924/merging-a-multi-dimensional-array-into-another-multi-dimensional-array/\">merging多维阵列到另一个多维数组

Same answer from the other question you posted relating this issue:- merging a multi-dimensional array into another multi-dimensional array

这篇关于如何创建多维数组的集合,当新推不覆盖原始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆