Re: Multi-Dimensional Arrays
| From: | Jonathan Duncan | Date: | Thu, 10 Oct 2002 23:44:23 +0000 |
| Subject: | Re: Multi-Dimensional Arrays | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-22331@lists.php.net to get a copy of this message | ||
Perfect, thank you very much Owen!!
"Owen Prime" <owen@noggin.com.au> wrote in message
news:20021010031246.26194.qmail@pb1.pair.com...
> Sounds like what your trying to do is:
>
> $cartArray[] = array("ItemNumber" => $itemnumber,
> "Brand" => $brand,
> "Quantity" => $itemqty,
> "ItemName" => $itemname);
>
> This will give you:
>
> $cartArray[0]["ItemNumber"] = $itemnumber;
> $cartArray[0]["Brand"] = $brand;
> $cartArray[0]["Quantity"] = $itemqty;
> $cartArray[0]["ItemName"] = $itemname;
>
> Running the very top statement again will give you:
>
> $cartArray[1]["ItemNumber"] = $itemnumber;
> $cartArray[1]["Brand"] = $brand;
> $cartArray[1]["Quantity"] = $itemqty;
> $cartArray[1]["ItemName"] = $itemname;
>
> Note that $cartArray[] = $val is just a short hand way of doing
> array_push($cartArray, $val);
>
> Cheers,
>
> Owen Prime
> http://www.noggin.com.au
>
>
>
> Jonathan Duncan wrote:
>
> > I am trying to create an array to hold shopping cart information. The
> > array
> > I am using is called "cartArray". What I want to do is to define a
> > sub-array of cartArray with the information from one product. Then the
> > next time a product is added it appends then new information as a second
> > sub-array to cartArray and so forth. Following is some code I have been
> > using to test with and so far PHP will let me use ".=" to append but
when
> > I try to call it back with "print_r" or echo array[][] only the first
> > entry is
> > returned. Any ideas what I am doing wrong?
> >
> > --------------------------------------------
> > $brand="Brand1";
> > $itemnumber="456789";
> > $itemname="Some Item Name";
> > $itemqty=3;
> > $cartArray[] .= array(0=>array($itemnumber=>"$brand",
> > "$itemqty",
> > "$itemname"));
> > print_r($cartArray)."<BR><BR>";
> > $brand="Brand2";
> > $itemnumber="123456";
> > $itemname="Another Item Name";
> > $itemqty=9;
> > array_push($cartArray, array($itemnumber=>"$brand", "$itemqty",
> > "$itemname"));
> > print_r($cartArray)."<BR><BR>";
> > echo $cartArray[0][0]."<BR><BR>";
> > --------------------------------------------
> >
> > Thank you,
> > Jonathan Duncan
>
>