【问题标题】:PHP: Is this the right way to create an array by using a loop?PHP:这是使用循环创建数组的正确方法吗?
【发布时间】:2013-03-22 08:45:52
【问题描述】:

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

到目前为止我有:

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 变量,这可能是错误的。最终结果应该是这样的:

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

感谢所有帮助!

【问题讨论】:

    标签: php arrays json for-loop


    【解决方案1】:

    您必须将一个新子级附加到 $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
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      这样使用

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

      【讨论】:

      • 我猜这应该在循环中?
      【解决方案3】:

      尝试使用

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

      【讨论】:

      • 变量$j仍未使用。
      • @Raz 抱歉不清楚 .. 我们将 $j 初始化为 0 并定义了终结符 $qty ... 可能我没听懂你想说什么。
      【解决方案4】:

      您没有附加到 $cart 变量,而是在循环的每一次通过时覆盖它。

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

      $cart[]=...
      

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

      $cart=array();
      

      【讨论】:

        猜你喜欢
        • 2012-06-24
        • 1970-01-01
        • 1970-01-01
        • 2015-12-15
        • 2012-06-19
        • 2016-07-30
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多