0

I'm using PayPal's _cart form to take payment on a site I am building. I'm then using the notify_url to post the submitted fields to a PHP page that will in turn use the SendGrid API to send a confirmation email. This all works fine, but what I am wanting to do is create an 'order summary' of the items purchased (which can be multiple).

foreach ($i = 1; $i <= $_POST['num_cart_items']; $i++) {
   $name = $_POST['item_name' . $i];
   $number = $_POST['item_number' . $i];
   $quantity = $_POST['quantity' . $i];
}

What I am wanting to do is build an array out of the above so I can use $cart as $order, for example. Is it possible to make the above foreach build into an array?

1
  • 2
    Yes, you just push each value on to a new array. Commented Jul 12, 2017 at 21:35

1 Answer 1

2

Use this:

$cart = array();
for ($i = 1; $i <= $_POST['num_cart_items']; $i++) {
   $name = $_POST['item_name' . $i];
   $number = $_POST['item_number' . $i];
   $quantity = $_POST['quantity' . $i];
   $cart[] = [
       'name'    => $name,
       'number'  => $number,
       'quantity'=> quantity
       // you can add more 'key'=>$value pairs here
   ];
}

Now you can do foreach($cart as $order) { ... } where $order is the array containing the above keys

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

Comments

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.