Php 循环并连接同一数组

Php 循环并连接同一数组,php,arrays,Php,Arrays,我尝试在foreach中连接一个变量数组 Foreach (item as $orderitem) { //for each item I have to get the array below $prodord[] .= array( 'variant_id' => $variant_id, 'quantity' => 2 ); $orderData = array('order' => array( 'line_items' => ar

我尝试在foreach中连接一个变量数组

Foreach (item as $orderitem) {
//for each item I have to get the array below
$prodord[] .= array(
        'variant_id' => $variant_id,
        'quantity' => 2
);
$orderData = array('order' => array(
'line_items' => array(
//the following variable is when I need to put the array if is one item
//or two arrays if are two item        
$prodord
/*array(
        'variant_id' => $variant_id,
        'quantity' => 1
    )*/
)
));
我尝试将数组连接起来,使其具有如下值的变量

//First item 
array(
    'variant_id' => 123456,
    'quantity' => 1
),
//2d item
array(
    'variant_id' => 654321,
    'quantity' => 1
)
但是我的输出看起来像

array(2) {
[0]=>
string(5) "Array"
[1]=>
string(5) "Array"
}

有一个项目,它工作得非常完美。

记住,在php中使用变量时,不需要使用[this]将值插入变量,例如,如果变量声明为数组,则只需要推送下一个值

$prodord = array(); //Here we declared the Var as array
在这之后,我们可以使用这个变量到'for'或'foreach'或任何你想要的地方,并像这个例子一样连接起来

array_push($prodord, array('variant_id' => $variant_id,'quantity' => 1));

在这一行中,我们向数组$prodord推送一个新值,你可以随时这样做,如果你看的话,我向数组中推送一个包含值和变量的数组来获取信息

只需忘记点
,即用于字符串串联:
$prodord[]=array(……
语法创建一个新的数组元素。