Page 1 of 1
Array - 2 dimensional ones
Posted: Tue Oct 12, 2004 4:57 pm
by dubs
I'm in the process of learning PHP and I'm currently trying to get to grips with multidimensional arrays.
Basically I've got a two dimensional array with each row having a total of 3 columns which are product ; price and quantity. These arrays will need to be stored in a session as they will eventually form part of a shopping cart.
For example:
$HTTP_SESSION_VARS['cart'] = array(array('Bag of Nails','2.00','10'));
I now have an array called cart with one row and three columns.
What I can't work out is:
1. How to dynamically add another row to this array
2. How to dynamically find the product attribute and change the quantity.
I found this very easy in coldfusion, just haven't got to grips with accessing arrrays in PHP. can someone help me out
Richard
Posted: Tue Oct 12, 2004 5:26 pm
by potsed
1. How to dynamically add another row to this array
$HTTP_SESSION_VARS['cart'] = array(array('Bag of Nails','2.00','10'));
you could get this by putting in a loop...
$HTTP_SESSION_VARS['cart'][0] = array('Bag of Nails','2.00','10');
$HTTP_SESSION_VARS['cart'][1] = array('Bag of hammers','4.00','20');
$HTTP_SESSION_VARS['cart'][2] = array('Bag of glue','6.00','30');
or
$HTTP_SESSION_VARS['cart'] = array(
array('Bag of Nails','2.00','10'),
array('Bag of hammers','4.00','20'),
array('Bag of glue','6.00','30'));
both are the same...
2. How to dynamically find the product attribute and change the quantity.
$HTTP_SESSION_VARS['cart'][0][0] == 'Bag of Nails';
$HTTP_SESSION_VARS['cart'][0][1] == '2.00';
$HTTP_SESSION_VARS['cart'][0][2] == '10';
Posted: Tue Oct 12, 2004 5:32 pm
by potsed
sorry 4got 2 mention use the in_array() function
if (in_array('Bag of Nails', $HTTP_SESSION_VARS['cart'][0])) {
// whatever you want 2 do...
}
Posted: Tue Oct 12, 2004 7:56 pm
by John Cartwright
$HTTP_SESSION_VARS['cart'][0] = array('Bag of Nails','2.00','10');
$HTTP_SESSION_VARS['cart'][1] = array('Bag of hammers','4.00','20');
$HTTP_SESSION_VARS['cart'][2] = array('Bag of glue','6.00','30');
should be
Code: Select all
<?php
while (1) { //whatever your loop may be
$_SESSION['cart'][] = array('name'=>"$tool", 'price'=>"$price",'someothervar'=>"$somevar");
}
?>
Posted: Tue Oct 12, 2004 8:29 pm
by Christopher
If you do
$HTTP_SESSION_VARS['cart'] = array(array('Bag of Nails','2.00','10'));
then:
$HTTP_SESSION_VARS['cart'][0][0] == 'Bag of Nails'
$HTTP_SESSION_VARS['cart'][0][1] == '2.00'
$HTTP_SESSION_VARS['cart'][0][2] == '10'
Posted: Wed Oct 13, 2004 12:23 am
by harsha
Why don't u use array_push to push dynamically
A little confused now...so many replies
Posted: Wed Oct 13, 2004 3:46 am
by dubs
$HTTP_SESSION_VARS['cart'][0] = array('Bag of Nails','2.00','10');
$HTTP_SESSION_VARS['cart'][1] = array('Bag of hammers','4.00','20');
$HTTP_SESSION_VARS['cart'][2] = array('Bag of glue','6.00','30');
If I wanted to add another row would i simply increment cart by 1 and if so could you possible give me a clue how to do it.
Posted: Wed Oct 13, 2004 4:04 am
by twigletmac
Unless you are using PHP < 4.1 then use $_SESSION instead of $HTTP_SESSION_VARS.
Also can you all ('cept Phenom who does it anyway) remember to use the
tags around your PHP code.
Mac