开发者

PHP: Mix part of an array with another array

I've got an array of variable size wich is going to contain items

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_126265084123开发者_如何学C859_100002211034371_220039_5639038_n.jpg
        [infos] => This is the product name of Item 2
        [subtotal] => 191.1
    )
)

And I would like to take part of this array ([qty],[price],[infos]) for each items (no matter of few or many there are) and add it to another array which is formed as follow:

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
)

The result should look like shown below, so for each item that is going to be added to the array, [item_name_X], [quantity_X] and [amount_X] should increase.

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
 )


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

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

       etc


 }


If i were you, i would actually do it like this:

    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
       ),
       ...
    )

)


I would suggest looping through the items adding the fields of intrest to a sub array:

foreach ($itemsArray as $item) {

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

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

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


I suggest you append the entire first array to the second one like this :

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
                    )
                )
)

You are going to be able to access any product like this :

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

Hence no restriction of number of items, and no restriction on the data you need to access.

Plus, no parsing is needed.

So in the end :

$array2['products'] = $array1;

Should do the trick.


By principle, this is how user187291 answered while some little differences: First creating an array which solely contains the elements you would like to merge into the cart array and then merging it.

Additionally it contains a map so that you can specify which elements from the first array should be taken over by with which name:

# 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;

Just reading your comment, you can wrap it into a function, which I think is a good idea:

$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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜