Page 1 of 1

Simple Array

Posted: Thu Jul 07, 2005 12:46 pm
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]

Posted: Thu Jul 07, 2005 12:54 pm
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?

Posted: Thu Jul 07, 2005 1:02 pm
by Burrito

Posted: Thu Jul 07, 2005 2:00 pm
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...

Posted: Thu Jul 07, 2005 2:23 pm
by IceMetalPunk
Look at the function array_sum: http://www.php.net/manual/en/function.array-sum.php.

-IMP ;) :)

Posted: Thu Jul 07, 2005 2:34 pm
by Jr
cool, exactly what I was looking for. Thanks again guys.