Solved

neeed array() to have 3 elements

Posted on 2007-11-14
12
266 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:DavidWare
  • 5
  • 5
  • 2
12 Comments
 
LVL 17

Expert Comment

by:nplib
ID: 20281792
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
 

Author Comment

by:DavidWare
ID: 20281889
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
 
LVL 11

Expert Comment

by:AlexanderR
ID: 20281914
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Assisted Solution

by:AlexanderR
AlexanderR earned 25 total points
ID: 20281931
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
 
LVL 17

Expert Comment

by:nplib
ID: 20282003
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
 

Author Comment

by:DavidWare
ID: 20282024
Well, does it make sence for me to make 2 arrays
("Item" => Price)
("Item" => Quantity)
then loop through both to update the cart?

0
 
LVL 17

Expert Comment

by:nplib
ID: 20282031
you can also do that if you wish
0
 
LVL 17

Expert Comment

by:nplib
ID: 20282132
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
 

Author Comment

by:DavidWare
ID: 20282172
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
 

Author Comment

by:DavidWare
ID: 20282184
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
 
LVL 17

Accepted Solution

by:
nplib earned 75 total points
ID: 20282905
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
 

Author Comment

by:DavidWare
ID: 20283891
Thank you very much, both of you.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question