2013-03-22 83 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(); 
相关问题