PHP - 尝试创建一个数组似乎不起作用

时间:2016-08-04 12:21:24

标签: php arrays

这是代码:

function create_order_with_custom_products()
{
    $orderGenerator = new OrderGenerator();
    $orderGenerator->setCustomer(6907);

    $orderGenerator->createOrder(array(
        // Add configurable product
        array(
            'product' => 30151,
            'qty' => 1
        ), array(
            'product' => 30150,
            'qty' => 2
        ),
    ));
}

我必须创建具有这种结构的数组:

        array(
            'product' => 30151,
            'qty' => 1
        ), array(
            'product' => 30150,
            'qty' => 2
        ),

我正在尝试创建一个具有相同结构的数组:

foreach($ItemsInCart['productid'] as $key=>$value){
    $ProductId = $value;
    $ProductQty = $ItemsInCart["productqty"][$key];
    $product_id = $ProductId; // Replace id with your product id
    $qty = $ProductQty; // Replace qty with your qty

    $ItemsId[] = ['product' => $ProductId, 'qty' => $ProductQty];
}

这给了我以下结果:

  

数组([0] =>数组([product] => 30143 [数量] => 1)[1] =>数组(   [product] => 30144 [数量] => 2)[2] =>数组([product] => 30145   [qty] => 3)[3] =>阵列([product] => 30146 [qty] => 4)[4] =>   数组([product] => 30147 [qty] => 5))

我只想知道为什么:

function create_order_with_custom_products()
{
    $orderGenerator = new OrderGenerator();
    $orderGenerator->setCustomer(6907);

    $orderGenerator->createOrder(array(
        // Add configurable product
        array(
            'product' => 30151,
            'qty' => 1
        ), array(
            'product' => 30150,
            'qty' => 2
        ),
    ));
}

与此不一样:

function create_order_with_custom_products()
{
    $orderGenerator = new OrderGenerator();
    $orderGenerator->setCustomer(6907);

    $orderGenerator->createOrder(array($ItemsId));
}

为什么第二种方法不起作用,我的错误在哪里?

2 个答案:

答案 0 :(得分:2)

最简单的方法是

$ItemsId = []; //declaration
$ItemsId [] = ["product"=>30151,"qty"=>1]; //pusing an array with key 'product' & 'qty' to $ItemsId

更新

这一行

$orderGenerator->createOrder(array($ItemsId));

不起作用,因为$ItemsId已经是一个数组,并且你将它放在一个带有array($ItemsId)的新数组中。这看起来像

Array([0] => 
     Array ([0] =>  
            Array(
                 [0] =>Array  ( [product] => 30143,[qty] => 1 ) 
                 [1] => Array ( [product] => 30144,[qty] => 2 ) 
                 [2] => Array ( [product] => 30145,[qty] => 3 ) 
                 [3] => Array ( [product] => 30146,[qty] => 4 ) 
                 [3] => Array ( [product] => 30147,[qty] => 5 ) 
            )
      )
)

但是预期的数组应该看起来像

      Array ([0] =>  
             Array(
                   [0] =>Array  ( [product] => 30143,[qty] => 1 ) 
                   [1] => Array ( [product] => 30144,[qty] => 2 ) 
                   [2] => Array ( [product] => 30145,[qty] => 3 ) 
                   [3] => Array ( [product] => 30146,[qty] => 4 ) 
                   [3] => Array ( [product] => 30147,[qty] => 5 ) 
             )
       )

要解决此问题,只需更改

即可
$orderGenerator->createOrder(array($ItemsId)); 

$orderGenerator->createOrder($ItemsId);

答案 1 :(得分:0)

使用计数器确保数组的两个子元素共享相同的索引。像这样:

$i = 0;

foreach(...) {
...
$ItemsId[$i]["product"] = $ProductId;             
$ItemsId[$i]["qty"] = $ProductQty;
...
$i++;
}