Simple Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jr
Forum Commoner
Posts: 99
Joined: Mon Mar 07, 2005 3:25 pm

Simple Array

Post by Jr »

Ok... I have a normal array with some names and I have some values for those names in an another array. I can loop through them fine and print them out accordingly as needed.

My problem is that I can't figure out how to Add all the values together. It seems like it should be easy but I'm obviously doing something wrong but I can't figure it out. Help? Please?

Code: Select all

$bill_names = array( &quote;One&quote;, &quote;Two&quote;, &quote;Three );
$bill_values = array( 11, 22, 33 );
[/b]
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Your question is really badly written so I am guessing what you are trying to ask!

Code: Select all

$bill_names = array( "One", "Two", "Three );
$bill_values = array( 11, 22, 33 );

$i = 0;
foreach($bill_names as $key){
	$combined_array[$key] = $bill_values[$i];
	$i++;
	}
Is that what you want?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Jr
Forum Commoner
Posts: 99
Joined: Mon Mar 07, 2005 3:25 pm

Post by Jr »

sorry, I didn't mean combine the two different arrays... I meant to take the values of the $values array and add those numbers together. Oops...
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Look at the function array_sum: http://www.php.net/manual/en/function.array-sum.php.

-IMP ;) :)
Jr
Forum Commoner
Posts: 99
Joined: Mon Mar 07, 2005 3:25 pm

Post by Jr »

cool, exactly what I was looking for. Thanks again guys.
Post Reply