Array - 2 dimensional ones

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Array - 2 dimensional ones

Post 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
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post 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';
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post 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...
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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");

}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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'
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

Why don't u use array_push to push dynamically
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

A little confused now...so many replies

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply