2013-03-22 79 views
0

我正在研究這個電子商務網站,我試圖創建一個包含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 
    ) 
); 

所有幫助表示讚賞!

回答

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

太棒了!謝謝! :) – Ismailp 2013-03-22 09:08:42

0

使用這樣

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

我想這應該是在循環? – Ismailp 2013-03-22 08:49:41

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

變量'$ j'仍未使用。 – Rayne 2013-03-22 08:52:17

+0

@Raz對不起沒有清楚..我們正在初始化$ j到0和終結者$ qty被定義...可能我不明白你想說什麼。 – alwaysLearn 2013-03-22 08:57:04

+0

你說得對。對不起。 – Rayne 2013-03-22 09:01:27

0

你是不是追加到$車變量,要覆蓋它在循環的每次傳球。

使用[]語法追加到數組:

$cart[]=... 

此外,其良好的聲明在代碼的頂部空數組:

$cart=array(); 
相關問題