Pulling $_GET data and creating multidimensional array using loop

Posted by Chris J on Stack Overflow See other posts from Stack Overflow or by Chris J
Published on 2012-10-12T09:28:30Z Indexed on 2012/10/12 9:37 UTC
Read the original article Hit count: 194

Filed under:
|
|

I'm creating a checkout for customers and the data about what's in their cart is being sent to a page (for just now) via $_GET.

I want to extract that data and then populate a multidimensional array with it using a loop.

Here's how I'm naming the data:

$itemCount = $_GET['itemCount'];
$i = 1;
while ($i <= $itemCount) {
  ${'item_name_'.$i} = $_GET["item_name_{$i}"];
  ${'item_quantity_'.$i} = $_GET["item_quantity_{$i}"];
  ${'item_price_'.$i} = $_GET["item_price_{$i}"];
  //echo "<br />Name: " .${'item_name_'.$i}. " - Quantity: " .${'item_quantity_'.$i}. " - Price: ".${'item_price_'.$i};
  $i++;
}

From here I'd like to create a multidimensional array like such:

Array
(
[Item_1] => Array
  (
  [item_name] => Shoe
  [item_quantity] => 2
  [item_price] => 40.00
  )
[Item_2] => Array
  (
  [item_name] => Bag
  [item_quantity] => 1
  [item_price] => 60.00
  )
[Item_3] => Array
  (
  [item_name] => Parrot
  [item_quantity] => 4
  [item_price] => 90.00
  )
  .
  .
  .
)

What I'd like to know is if there is a way I can create this array in the existing while loop? I'm aware of being able to add data to an array like $data = [] after delacring an empty array but the actual syntax eludes me.

Maybe I'm completely off the right track and there is a better way of doing it?

Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about loops