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!

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

Use like this

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

Try Using

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

You are not appending to the $cart variable, you are overwriting it on every pass of the loop.

Use the [] syntax to append to an array:

$cart[]=...

Also, its good to declare an empty array at the top of the code:

$cart=array();