Jump to content


Photo

Associating Arrays


  • Please log in to reply
3 replies to this topic

#1 wft2

wft2
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 March 2006 - 07:22 AM

I have tried looking this up in the PHP manual, online, and in a book I have but I can't seem to figure out what method to use for the following:

I have a form that when submitted creates an array named "products." This array contains varioius product names. When submitted it contains something like this:
Product_A, Product_B, Product_C

I have another array (associative one that I created) named "Prices." It contains product names as the keys and their prices as the values. It was created like this:
$Prices["Product_A"] = 95;
$Prices["Product_B"] = 174;
$Prices["Product_C"] = 185;
$Prices["Product_D"] = 103;
$Prices["Prodcut_E"] = 220;


I am trying to find a way to automatically calculate a total price for all the products in the "products" array based on the values (prices) found in the "prices" array. The products array will be different each time the form is submitted. I am not sure how to accomplish this. I don't know if I need a command that automatically creates a new array that contains the prices and then do a sum of it, or if there is a better method that involves creating a variable of some kind.

If I could just find a way to create a new array that in this case contains 95, 174, 185 I would be happy. I know how to do a sum on an array.

Can anybody point me in the right direction? I am going about this all wrong?

Thanks!

#2 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 11 March 2006 - 08:14 AM

$total = 0;

while (list($key,$val)=each($products)) {
  $total += $prices[$val];
}
echo $total;

I think this might be what you are looking for.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#3 wft2

wft2
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 March 2006 - 02:50 AM

[!--quoteo(post=353872:date=Mar 11 2006, 03:14 AM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 11 2006, 03:14 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$total = 0;

while (list($key,$val)=each($products)) {
  $total += $prices[$val];
}
echo $total;

I think this might be what you are looking for.
[/quote]


That is exactly what I was trying to do. I had tried doing a "foreach" loop and I couldn't make that work. But the "while" loop that you provided worked perfectly.

Thanks for your help. I appreciate it.


#4 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 12 March 2006 - 05:26 AM

It could be done with a for each, but I normally give while statements for examples. Either way works.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users