我正在研究這個電子商務網站,我試圖創建一個包含PHP中的購物車項目的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 = 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
)
);
所有幫助表示讚賞!
太棒了!謝謝! :) – Ismailp 2013-03-22 09:08:42