dssjxvbv918586 2013-03-22 08:45
浏览 23
已采纳

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

I'm working on this e-commerce site and I'm trying to create a jSON array containing the cart items in PHP.

So far I have:

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
    );
  }
}

I know I have the $cart var inside the loop which is probably wrong. The end result should be like this:

$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
    )
);

All help is appreciated!

  • 写回答

4条回答 默认 最新

  • douhoushou8385 2013-03-22 08:50
    关注

    You have to append a new child to $cart instead of overwriting it. To append values to an array (the easy way), use $array[] = …. PHP increments the child's ID automatically.

    Not required, but please initialize $cart first and use descriptive variables.

    To inspect an array (or other data), use 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
        );
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大