[英]Loop and concatenate same Array
我尝试在一个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中,并且您可以随时随地执行此操作,如果您看我将包含值和变量的数组推送到数组中以获取信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.