0

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_126265084123859_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] => [email protected]
    [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] => [email protected]
    [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
 )
3
  • 1
    1) What have you tried? 2) What you propose to do sounds like a bad idea. Tomorrow you may be be asking how to parse the quantity_X items back into an array. Commented Sep 16, 2011 at 7:33
  • 1
    Hi Jon and thanks for the help! I'm a graphic artist and I'm just starting programming. This is why I'm asking if this is doable and should be done. So far, from reading the PHP manual, I made a copy of my array and removed the data I didn't need and tried to merge it with the other array using array_merge. What I'm trying to do is creating a form to submit to paypal checkout with the data above. Instead of trying to merge the array, should I rather create a function that take the as arguments the data from array 2 and pass array 1 as argument of the function and build the form from there? Commented Sep 16, 2011 at 8:00
  • If you are just going to feed the data to paypal then it's fine (I gave a general warning since I didn't know the context). Try out some of the answers below. Commented Sep 16, 2011 at 8:01

5 Answers 5

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

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

       etc


 }
Sign up to request clarification or add additional context in comments.

2 Comments

Hi stereofrog and thanks for posting. This sounds like a great solution. I'll go and try this.
I just tried it... and it's working nicely. I cannot beleive it was that easy! I'm glad I finally asked about this. Thanks a lot stereofrog!
0

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

    Array
(
    [cmd] => _cart
    [upload] => 1
    [business] => [email protected]
    [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
       ),
       ...
    )

)

1 Comment

Hi NineAllexis. I guess this would make sense. But I'm trying to have a simple array as key=> value so I could build a form to post to paypal checkout. I won't need to access the data from that array after this.
0

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

1 Comment

Thanks Adam! I've used stereofrog code posted above... because it's short, I understand what it does and he got here first. :o) Thanks for the help!
0

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

Array
(
    [cmd] => _cart
    [upload] => 1
    [business] => [email protected]
    [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.

1 Comment

Thanks a lot Thomas! I'm simply creating the array to SSL encode the data and pass it to paypal checkout so a simple array is easier in my case. I should have been mentioning this earlier. Anyways, thanks for the help!
0

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

2 Comments

Thanks a lot hakre. Amazing to see so many ways there are to solve a problem.
There is always more than one way :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.