Solved

neeed array() to have 3 elements

Posted on 2007-11-14
12
262 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now