Re: multi dimensional array producing a parse error
| From: | Steve Edberg | Date: | Tue, 16 Oct 2001 20:34:44 +0000 |
| Subject: | Re: multi dimensional array producing a parse error | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71257@lists.php.net to get a copy of this message | ||
Oops, guess I didn't read close enough here...
At 12:01 PM -0700 10/16/01, Steve Edberg wrote:
At 11:20 AM -0700 10/16/01, Sheila Myers wrote:So the structure of your $items array will be [0] => array(I'm struggling with using a multidimensional array in my shopping cart class. Can anyone tell me why this code is generating a parse error at the indicated line? Thanks, Sheila Class Cart { var $items = array("productid"=>'', "details"=>array( "price"=>'', "quantity"=>'', "shipping"=>'', "tax"=>'', "desc"=>'', "note"=>'') ); var $product_total; var $shipping_total; var $tax_total; var $cart_id;
'productid' => <product id>,
'details' => array(
....a bunch of stuff...
),
),
[1] => array(
'productid' => <product id>,
'details' => array(
....a bunch of stuff...
),
)
...etc.
I suspect you're _really_ wanting something like
<product id> => array(
'price' => <price>,
...and so on...
)
Incidentally, because of PHP's loose typing, you don't have to declare $items as an array; although, by doing as you have, it's a bit more self-documenting. It does leave you with an array of size one, though - where all the elements are empty.
If you use the array as I mentioned above, this code block could be $this->items[$productid] = array( 'price' => $sell_price, ...and so on... ); OR, you could leave out the line above and simply use your code below unaltered. If you wanted to use your original structure, you could do something like $this->items[] = array( 'productid' = $productid, 'details' = array( 'price' => $sell_price, ...and so on... ) ); Does that make more sense?? Because of PHP's array flexibility, it can be easy to confuse oneself at times! - steve, againfunction add($sell_price,$qty,$shipping,$tax,$productid,$short_desc,$note) { /* add an item to the shopping cart and update the total price */ if (isset($this->items[$productid])) //if it is already in the cart, don't add a new occurence, just update the total quantityEasy; don't use the => construct here (and in the following lines). That's only for use in the array() function. Just use $this->items["productid"] = $productid; here and in the lines below.{ $this->items[$productid]["quantity"] += $qty;} else { $this->items["productid"]=>$productid; <<<==================this is the line reporting the parse error
-steve-- +--------------- my people are the people of the dessert, ---------------+ | Steve Edberg sbedberg@ucdavis.edu | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | +---------------- said t e lawrence, picking up his fork ----------------+$this->items[$productid]["price"]=>$sell_price; $this->items[$productid]["quantity"]=>$qty; $this->items[$productid]["shipping"]=>$shipping; $this->items[$productid]["tax"]=>$tax; $this->items[$productid]["desc"]=>$short_desc; $this->items[$productid]["note"]=>$note; $this->product_total = $this->product_total += $sell_price; $this->shipping_total = $this->shipping_total += $shipping; } var_dump $this->items; //debugging } //end add