Re: Multi-Dimensional Arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux