Solved

neeed array() to have 3 elements

Posted on 2007-11-14
12
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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