0

我有一个产品数组,它的名字是 $item

    Array
(
    [0] => Array
        (
            [Quantity] => 2
            [Product] => Array
                (
                    [Name] => Barbula klandoa  - Caryopteris clandonensis  
                    [UnitPrice] => Array
                        (
                            [Gross] => 1480
                            [Net] => 0
                            [Tax] => 0
                            [TaxRate] => 0
                            [CurrencyCode] => PLN
                        )

                )

        )

    [1] => Array
        (
            [Quantity] => 1
            [Product] => Array
                (
                    [Name] => Aronia czarnoowocowa Nero - Aronia melanocarpa Nero
                    [UnitPrice] => Array
                        (
                            [Gross] => 1200
                            [Net] => 0
                            [Tax] => 0
                            [TaxRate] => 0
                            [CurrencyCode] => PLN
                        )

                )

        )

    [2] => Array
        (
            [Quantity] => 1
            [Product] => Array
                (
                    [Name] => Ambrowiec Amerykański P9 - Liquidambar styraciflua
                    [UnitPrice] => Array
                        (
                            [Gross] => 1300
                            [Net] => 0
                            [Tax] => 0
                            [TaxRate] => 0
                            [CurrencyCode] => PLN
                        )

                )

        )

)

不,我必须把它传递给这个:

    $shoppingCart = array(
    'GrandTotal' => ($suma_z_produktow*10),
    'CurrencyCode' => 'PLN',
    'ShoppingCartItems' => array (

                array ('ShoppingCartItem' => $item)

    )
);

结果是只有来自这个数组的最后一个条目被传递给这个新数组。我可以 nodifi 并像这样传递它:

$shoppingCart = array(
    'GrandTotal' => ($suma_z_produktow*10),
    'CurrencyCode' => 'PLN',
    'ShoppingCartItems' => array (

                array ('ShoppingCartItem' => $item[0)
    array ('ShoppingCartItem' => $item[1)

    )
);

该方法有效,但我不知道客户将订购多少产品。有什么方法可以传递 1 行中的所有项目吗?

它用于 Payu 支付方式集成。谢谢

4

2 回答 2

2

我认为你需要调整你的代码来让它做你想做的事。以下是我的建议:

$shoppingCart = array(
    'GrandTotal' => ($suma_z_produktow*10),
    'CurrencyCode' => 'PLN',
    'ShoppingCartItems' => array (),
);

foreach($item as $product) {
    $shoppingCart['ShoppingCartItems'][] = array('ShoppingCartItem' => $product);
}
于 2013-06-28T13:25:13.983 回答
1

尝试这个:

$shoppingCart = array(
    'GrandTotal' => ($suma_z_produktow*10),
    'CurrencyCode' => 'PLN',
    'ShoppingCartItems' => array (),
);

foreach($item as $cartItem){
    $shoppingCart['ShoppingCartItems'][] = array('ShoppingCartItem'=>$cartItem);
}

print_r($shoppingCart);

这将建立shoppingCartItems尽可能多的项目。

于 2013-06-28T13:24:33.607 回答