将整个Array传递给另一个数组中的php

时间:2013-06-28 13:15:33

标签: php arrays payment-gateway

我的产品数组名为$ 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)

    )
);

结果是只有此数组的最后一个条目传递给此新数组。 我可以点头并将其传递出去:

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

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

    )
);

该方法有效,但我不知道客户订购了多少产品。有没有方法可以传递一行中的所有项目?

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

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分: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