Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy Wat to Add Values in an Array - Please

Status
Not open for further replies.

david6633

Programmer
Jul 28, 2003
39
GB
I have, for example, and array containing the following:
Code:
row1_value1, row1_value2, row2_value1, row2_value2, ..., rown_value1, rown_value2
The question is - is there an easy way to add all the "_value1" to get a total and all the "_value2" to get another total without
Code:
$total1 = $row1_value1 + $row2_value1 + ... + $rown_value1

David
Remember: You only know what you know
and - you don't know what you don't know!
 
that does not seem like a great way to construct an array. if your data is multi-dimensional then why not construct the array as a multi-dimensional?

as a kludge... and i am assuming that your references above are to keys within the array e.g your actual array looks like
Code:
array( "row1_value1" => 2,
       "row1_value2" => 300,
       "row2_value1" => 43,
       "row3_value2" => 122);
//etc

Code:
$n =  ;// enter value for n: i.e. $row[red]n[/red]
$v = ; // enter value for v : i.e ..._value[red]v[/red]
for ($i=1; $i<=n; $i++){
  $sum[$i] = 0;
  for ($j=1; $j <= $v; $j++){
     $sum[$i] = $sum[$i] + $array["row{$i}_value{$j}"];
  }
}
print_r ($sum);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top