2017-03-09 65 views
0

我会再做连接的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" 
} 

有一个项目,它的作品完美。

+0

只要忘记点''',就是字符串连接:'$ prodord [] = array(.........'']'语法创建一个新的数组元素。 – AbraCadaver

回答

0

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

$prodord = array(); //Here we declared the Var as array 

此后,我们可以将此变量用于'for'或'foreach'或任何您想要的位置并像本示例一样连接;

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

所以在这条线,我们推一个新的价值,以我的数组$ prodord,你可以做你想做这种随时随地,如果你看我把一个数组与值和变量来获取信息的阵列。

+0

Thanks it作品 – javaTodev