PHP:将数组的一部分与另一个数组混合

时间:2011-09-16 07:30:39

标签: php arrays

我有一个可变大小的数组,它将包含项目

Array
(
[0] => Array
    (
        [id] => 126264027457298
        [qty] => 6
        [price] => 55.85
        [shipping] => 6.00
        [fbids] =>  12
        [imgids] => 126264027457298
        [albids] => 126263974123970
        [thumbs] => https://268088_126264027457298_100002211034371_220013_7172874_n.jpg
        [infos] => This is the product name of Item 1
        [subtotal] => 371.1
    )

[1] => Array
    (
        [id] => 126265084123859
        [qty] => 6
        [price] => 25.85
        [shipping] => 6.00
        [fbids] =>  11
        [imgids] => 126265084123859
        [albids] => 126263974123970
        [thumbs] => https://261288_126265084123859_100002211034371_220039_5639038_n.jpg
        [infos] => This is the product name of Item 2
        [subtotal] => 191.1
    )
)

我想为每个项目(无论少数或多个项目)参与此数组([qty],[price],[infos])的一部分,并将其添加到另一个数组中,该数组形成如下:

Array
(
    [cmd] => _cart
    [upload] => 1
    [business] => xxxxx@xxxxx.com
    [return] => http://www.mysite.com/paypal/return.php
    [cancel_return] => http://www.mysite.com/paypal/cancel.php
    [currency_code] => EUR
    [lc] => FR
)

结果应如下所示,因此对于要添加到数组中的每个项目,[item_name_X],[quantity_X]和[amount_X]应该增加。

Array
(
    [cmd] => _cart
    [upload] => 1
    [business] => xxxxx@xxxxx.com
    [return] => http://www.mysite.com/paypal/return.php
    [cancel_return] => http://www.mysite.com/paypal/cancel.php
    [currency_code] => EUR
    [lc] => FR
    [item_name_1] => This is the product name of Item 1
    [quantity_1] => 6
    [amount_1] => 55.85
    [item_name_2] => This is the product name of Item 2
    [quantity_2] => 6
    [amount_2] => 25.85
 )

5 个答案:

答案 0 :(得分:2)

foreach($firstArray as $number => $sub) {

     $secondArray["item_name_" . ($number + 1)] = $sub["infos"];
     $secondArray["qty_" . ($number + 1)] = $sub["qty"];

       etc


 }

答案 1 :(得分:0)

如果我是你,我会这样做:

    Array
(
    [cmd] => _cart
    [upload] => 1
    [business] => xxxxx@xxxxx.com
    [return] => http://www.mysite.com/paypal/return.php
    [cancel_return] => http://www.mysite.com/paypal/cancel.php
    [currency_code] => EUR
    [lc] => FR
    [products] => array(
       "ItemName1" => array( 
           "Qty" => 6
           "Total" => xxx
       ),
       ...
    )

)

答案 2 :(得分:0)

我建议循环遍历项目,将感兴趣的字段添加到子数组中:

foreach ($itemsArray as $item) {

    $wantedFields = array('qty','price','infos');
    $tempArray = array();

    foreach ($wantedFields as $field) {
        $tempArray[$field] = $item[$field];
    }

    $cartArray['products'][] = $tempArray;
}

答案 3 :(得分:0)

我建议你将整个第一个数组附加到第二个数组:

Array
(
    [cmd] => _cart
    [upload] => 1
    [business] => xxxxx@xxxxx.com
    [return] => http://www.mysite.com/paypal/return.php
    [cancel_return] => http://www.mysite.com/paypal/cancel.php
    [currency_code] => EUR
    [lc] => FR
    [products] = Array
                (
                [0] => Array
                    (
                        [id] => 126264027457298
                        [qty] => 6
                        [price] => 55.85
                        [shipping] => 6.00
                        [fbids] =>  12
                        [imgids] => 126264027457298
                        [albids] => 126263974123970
                        [thumbs] => https://268088_126264027457298_100002211034371_220013_7172874_n.jpg
                        [infos] => This is the product name of Item 1
                        [subtotal] => 371.1
                    )

                [1] => Array
                    (
                        [id] => 126265084123859
                        [qty] => 6
                        [price] => 25.85
                        [shipping] => 6.00
                        [fbids] =>  11
                        [imgids] => 126265084123859
                        [albids] => 126263974123970
                        [thumbs] => https://261288_126265084123859_100002211034371_220039_5639038_n.jpg
                        [infos] => This is the product name of Item 2
                        [subtotal] => 191.1
                    )
                )
)

您将能够访问以下任何产品:

$array2['products'][0]['id'], $array2['products'][1]['id']

因此不限制项目数量,也不限制您需要访问的数据。

另外,不需要解析。

所以最后:

$array2['products'] = $array1;

应该做的伎俩。

答案 4 :(得分:0)

原则上,这就是user187291 answered的一些小差异:首先创建一个数组,它只包含你想要合并到购物车数组中的元素,然后合并它。

此外,它包含一个地图,以便您可以指定应该使用哪个名称接管第一个数组中的哪些元素:

# create an array with the items to add

$addItems = array();
$map = array('infos' => 'item_name', 'qty' => 'quantity', 'price' => 'amount');
$count = 0;
foreach($firstArray as $product)
{
    $count++;
    foreach($map as $from => $to)
    {
        $addItems["$to_$count"] = $product[$from]
    }
}

# merge

$cart += $addItems;

只是阅读你的评论,你可以把它包装成一个函数,我认为这是一个好主意:

$cart = add_items_to_cart($items, $cart);

/**
 * add items to cart
 *
 * @param array $items items to add to cart
 * @param array $cart cart without items
 * @return array cart with items
 */
function add_items_to_cart(array $items, array $cart)
{
    # map
    $addItems = array();
    $map = array('infos' => 'item_name', 'qty' => 'quantity', 'price' => 'amount');
    $count = 0;
    foreach($items as $item)
    {
        $count++;
        foreach($map as $from => $to)
        {
            $addItems["$to_$count"] = $item[$from]
        }
    }

    # merge    
    $cart += $addItems;

    return $cart;
}