Unset array to specified form

Socius Decennalis
Decennium
Joined
Aug 15, 2008
Messages
489
Reaction score
43
Solved! I had to use array_map to make it work.
PHP:
$products_implode = implode(" ", array_map(function ($entry) { return $entry['amount'].'x '.$entry['name'].'<br />'; }, $products));
___________________________________________________________________________________________________________________

I'm using the PayPal IPN to receive name and quantity for each item that has been bought and add them to database.

I want them to be in 1 row, like "1x Item name, 2x Other item, 1x Another item", so I created an array and added items to it with for loop.

PHP:
$num_cart_items = $_POST['num_cart_items'];

$products= array();
for($i = 1; $i <= $num_cart_items; $i++) {
    $products[] = array('name'=>$_POST['item_name' . $i], 'amount'=>$_POST['quantity' . $i]);
}

How can I destruct this array and make a variable $allproducts that has the items as "1x Item name, 2x Other item, 1x Another item"?
 
Last edited:
Search PHP manual for implode/ explode.
I couldn't find explode being used for anything else than turning string into an array.

However, with implode I have this issue.
PHP:
$products = array();
$products[] = array('name'=>'Tuote 1', 'amount'=>'1');
$products[] = array('name'=>'Tuote 52', 'amount'=>'2');
echo implode(" ", $products);

Outputs "Array Array"

How can I implode an array inside an array?

EDIT: So I found out I had to use array_map for this. Solved.
 
Last edited:
Back