neeed array() to have 3 elements

I've learned how to make an array, which is like a hashtable with Key and Value (or Item, Price), but I'm making a shopping cart, so I need Item, Price, Quantity.  I can't add a third element.
I can reinvent the wheel here, but I'd like to learn the real way to do this in PHP.
Thanks,
David
DavidWareAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
nplibConnect With a Mentor Commented:
Yes you can use spaces,
and yes the amounts don't need to be in quotes as long as it's just the integer and not
$19.95, that would then be a string variable type.

$products = array(
      "Brown Shoes" => array(19.95, 0),
      "books" => array(10.95, 0),
      "pizza" => array(9.95, 0),
);
that way you only have to loop through one array
foreach ($products as $item => $values) {
    echo "I have $item number of ".$values['1']." that cost ".$values['0'].".<br />";
}

Yes this is an array of arrays,
the reason this is better, is if for whatever reason you had a null value, it won't throw of your key elemenets,
with a two arrays, it is possible that they could end up being different lengths.
0
 
nplibCommented:
Do you mean

$array = Array([$1.00] => "shoes")

or do you mean
$arrayPrice = Array([0] => "$1.00")
$arrayItems = Array([0] = > "shoes")

arrays can only one 1 value with 1 associations,

you can have a multidementional array

$array = Array(
    [0] = Array([$1.00] => "Shoes");
}

Can you give an example of what you want?
0
 
DavidWareAuthor Commented:
Hi nplib, thanks for replying.
I don't know for sure - I'm new to PHP.

I want to load an array with the values from 2 fields in my Products table, [Item] and [Price],and have a third element [Quantity] that defaults to 0 (zero).  

("shoes", 19.95, 0)
("book", 10.95, 0)
("pizza", 9.95, 0)
of course this isn't a hashtable with Key => Value, its a 2-dimentional array)

When the customer adds an Item to his cart, [Quantity] is incremented.
When the user views his cart, my code will populate a form with all items he's bought and offer him the option to update each item's quantity, which will rewrite the array.

Does that answer your question?
Thanks again!
David
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
AlexanderREnterprise Web DeveloperCommented:
Looks you've been trying to do a simple array like
['item']=>['price']

Got to agree, it would be nice if PHP could implement something like VB's data tables.
For now what you should do instead is to create a sepparate array for each item and then put all of them into one "global" array. like so

Array(
['item']=>(Array['price'] ="PRICE",
                         ['quantity']=10000)) OR

Array(
['id']=> (Array ['item'] => "ITEM  NAME",
                       ['price'] => "PRICE",
                       ['quantity'] => 100000)
)
where id is an increment/coutner            
0
 
AlexanderRConnect With a Mentor Enterprise Web DeveloperCommented:
I finished typing my commet after your comment so if you use mysql it would look something like

$i = 0;
while($rwProducts = myqli_fetch_array($rsYOUR_QUERY_RESULTS)){
$Card[$i]['item'] = $rwProducts['item'];
$Card[$i]['price'] = $rwProducts['price'];
$Card[$i]['quantity'] = $rwProducts['quantity'];
$i++;
}
0
 
nplibCommented:
you can't make an array like that,

arrays have a key and a value and that's it,

you will need to do one of the following

make three arrays

$priceArray = array("19.95", "10.95", "9.95");
$itemArray = array("Shoes", "books", "pizza");
$quantityArray = array(0, 0, 0);

Then reference them by numerical key

foreach ($itemArray as $key => $value) {
   echo "I have ".$quantityArray[$key]." number of $value that cost ".$priceArray[$key].".";
}

or create a multidimensional array

$products = array(
          array("19.95", "10.95", "9.95"),
          array("Shoes", "books", "pizza"),
          array(0, 0, 0)
);

Then you have to reference the array just like an regular array, just with more elements,

echo $products['0']['0']." ".$products['0']['1']." ".$products['0']['2'];

will output 19.95 shoes 0
0
 
DavidWareAuthor Commented:
Well, does it make sence for me to make 2 arrays
("Item" => Price)
("Item" => Quantity)
then loop through both to update the cart?

0
 
nplibCommented:
you can also do that if you wish
0
 
nplibCommented:
to keep things more organized I would do this.

$products = array(
      "Shoes" => array("19.95", 0),
      "books" => array("10.95", 0),
      "pizza" => array("9.95", 0),
);
that way you only have to loop through one array
foreach ($products as $item => $values) {
    echo "I have $item number of ".$values['1']." that cost ".$values['0'].".<br />";
}
0
 
DavidWareAuthor Commented:
Would this be an "embedded array"?
Arrays within an array?
I like this!

Can I use spaces in the key of the parent array (so I can use it as the description)?

"Brown Shoes" + array(19.95, 0)

(and would 19.95 really need double-quotes?)
0
 
DavidWareAuthor Commented:
Oops, I wish EE allowed me to edit a last post...

I meant

"Brown Shoes" => array(19.95, 0)
"Paperback Books" => array("10.95", 0),
"Cheese Pizza" => array("9.95", 0),
0
 
DavidWareAuthor Commented:
Thank you very much, both of you.
0
All Courses

From novice to tech pro — start learning today.