PHP:这是使用循环创建数组的正确方法吗?

时间:2013-03-22 08:45:52

标签: php arrays json for-loop

我正在开发这个电子商务网站,我正在尝试创建一个包含PHP中购物车项目的jSON数组。

到目前为止,我有:

for ($i=0; $i < count($_SESSION['cart']); $i++) {
  $prodid = $_SESSION['cart'][$i][0];
  $sizeId = $_SESSION['cart'][$i][1];
  $colorId = $_SESSION['cart'][$i][2];
  $qty = $_SESSION['cart'][$i][3];
  $inslagning = $_SESSION['cart'][$i][4];
  $wrapCost += ($inslagning == 'YES' ? 20 : 0);
  $row = get_product_buy($prodid, $sizeId, $colorId);
  $prodname = $row['prodname'];
  $color = $row['color'];
  $size = $row['size'];
  $prodCatid  = $row['catid'];
  $image = $row['biggerimage'];
  $box = $row['box_number'];

  for ($j=0;$j<$qty;$j++) {
    $cart = array(
        'reference' => '123456789',
        'name' => $prodname,
        'quantity' => $qty,
        'unit_price' => $price,
        'discount_rate' => 0,
        'tax_rate' => 2500
    );
  }
}

我知道循环中有$ cart var可能是错误的。最终结果应该是这样的:

$cart = array(
    array(
        'reference' => '123456789',
        'name' => 'Klarna t-shirt',
        'quantity' => 1,
        'unit_price' => $att_betala * 100,
        'discount_rate' => 0,
        'tax_rate' => 2500
    ),
    array(
        'reference' => '123456789',
        'name' => 'Klarna t-shirt',
        'quantity' => 1,
        'unit_price' => $att_betala * 100,
        'discount_rate' => 0,
        'tax_rate' => 2500
    )
);

感谢所有帮助!

4 个答案:

答案 0 :(得分:4)

您必须将新子项附加到$cart而不是覆盖它。要将值附加到数组(简单方法),请使用$array[] = …。 PHP会自动递增孩子的ID。

不是必需的,但请先初始化$cart并使用描述性变量。

要检查数组(或其他数据),请使用var_dump

// Initialize an empty array. Not needed, but correct to do.
$cart = array();

for ($i=0; $i < count($_SESSION['cart']); $i++) {
  $prodid = $_SESSION['cart'][$i][0];
  $sizeId = $_SESSION['cart'][$i][1];
  $colorId = $_SESSION['cart'][$i][2];
  $qty = $_SESSION['cart'][$i][3];
  $inslagning = $_SESSION['cart'][$i][4];
  $wrapCost += ($inslagning == 'YES' ? 20 : 0);
  $row = get_product_buy($prodid, $sizeId, $colorId);
  $prodname = $row['prodname'];
  $color = $row['color'];
  $size = $row['size'];
  $prodCatid  = $row['catid'];
  $image = $row['biggerimage'];
  $box = $row['box_number'];

  // Append products $qty times.
  for ($productCount=0; $productCount<$qty; $productCount++) {
    // Append a new product to $cart.
    $cart[] = array(
        'reference' => '123456789',
        'name' => $prodname,
        'quantity' => $qty,
        'unit_price' => $price,
        'discount_rate' => 0,
        'tax_rate' => 2500
    );
  }
}

答案 1 :(得分:0)

像这样使用

$cart[] = array(
    'reference' => '123456789',
    'name' => $prodname,
    'quantity' => $qty,
    'unit_price' => $price,
    'discount_rate' => 0,
    'tax_rate' => 2500
);

答案 2 :(得分:0)

尝试使用

for ($j=0;$j<$qty;$j++) {
$cart[] = array(
    'reference' => '123456789',
    'name' => $prodname,
    'quantity' => $qty,
    'unit_price' => $price,
    'discount_rate' => 0,
    'tax_rate' => 2500
);
}

$json_enc  = json_encode($cart);

答案 3 :(得分:0)

您没有附加到$ cart变量,而是在循环的每次传递中覆盖它。

使用[]语法附加到数组:

$cart[]=...

此外,最好在代码顶部声明一个空数组:

$cart=array();