Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php向数组中添加元素不起作用_Php_Arrays - Fatal编程技术网

php向数组中添加元素不起作用

php向数组中添加元素不起作用,php,arrays,Php,Arrays,我在数组中插入元素,元素插入正常,但只插入最后一个 我认为push_阵列可以解决这个问题,我尝试了一下 array_push($rowsItemAddit, $rowsItemAddit["item"]["acId"], $precio_row_addit->acId); 但我有一个错误 注意:未定义索引:item 我为最后一个元素工作的代码是 foreach($precios_row_addit as $precio_row_addit){ $rowsItemAddit["it

我在数组中插入元素,元素插入正常,但只插入最后一个

我认为push_阵列可以解决这个问题,我尝试了一下

array_push($rowsItemAddit, $rowsItemAddit["item"]["acId"], $precio_row_addit->acId);
但我有一个错误

注意:未定义索引:item

我为最后一个元素工作的代码是

foreach($precios_row_addit as $precio_row_addit){
    $rowsItemAddit["item"]["acId"] = $precio_row_addit->acId;
    $rowsItemAddit["item"]["acValues"]  = array ( 'acValue' => $precio_row_addit->acValue );                    
    }
有什么办法可以得到完整的阵列吗

最终结构应为:

[additionalColumnValues] => Array
    (
        [item] => Array
            (
                [acId] => 0
                [acValues] => Array
                    (
                        [acValue] => 10
                    )

            )
        [item] => Array
            (
                [acId] => 1
                [acValues] => Array
                    (
                        [acValue] => 10
                    )

            )               
        [item] => Array
            (
                [acId] => etc.
                [acValues] => Array
                    (
                        [acValue] => 10
                    )

            )
    )

        )

)

TIA

您需要这样编码

foreach ($precios_row_addit as $precio_row_addit) {
    $rowsItemAddit[] = array('item' => array(
            'acId' => $precio_row_addit->acId,
            'acValues' => array('acValue' => $precio_row_addit->acValue)
        ));
}

您总是试图覆盖相同的数组元素。 它不能有多个名为“item”的键

尝试这样的方法,其中项目索引是数字索引而不是“项目”:


这与他想要的结构不匹配,也不是我想要的。这里是我的结果数组([0]=>Array([acId]=>0)[1]=>Array([acValues]=>Array([acValues]=>10))[2]=>Array([acId]=>1)[3]=>Array([acValues]=>Array([acValue]=>K-CostCenter))$rowsItemAddit[]的问题是现在有两个不同的节点,嗯。。是 啊但是现在您知道了,您想要的结构在php中无法与数组一起工作,对吗?数组键是一个唯一的标识符,不能出现两次或两次以上。谢谢大家的回答,最后我用你们的想法来指导它。这是最后一段代码,我使用一个临时数组$wrk=array()$wrk[“acId”]=$precio\u row\u addit->acId$wrk[“acValues”][“acValue”]=$precio\U row\U addit->acValue$rowsItemAddit[“项目”]=每件$wrk;
foreach($precios_row_addit as $precio_row_addit){
    $rowsItemAddit[]["acId"] = $precio_row_addit->acId;
    $rowsItemAddit[]["acValues"] = array ( 'acValue' => $precio_row_addit->acValue );                    
}