Jump to content

easy way to total array rows


Q695

Recommended Posts

Does anyone know how to total  identical values in arrays easily?

 

I tried the following, and none did what I needed:

//echo "$sql_merge_stats<br>";
print_r($row_merge_stats);
$total=array_merge($row_merge_stats);

echo "<p>";

print_r($total);
/////////////
echo "<p>";

$result_merge_stats = mysql_query($sql_merge_stats, $con) or die ("can not generate result " . mysql_error());
while ($row_merge_stats = mysql_fetch_assoc($result_merge_stats)){

print_r($row_merge_stats);
echo "<br>";
/////////////////

$total=$total+$row_merge_stats;

}
echo "<br>";
print_r($total);

//erroring
$result_merge_stats = mysql_query($sql_merge_stats, $con) or die ("can not generate result " . mysql_error());
//$row_merge_stats = mysql_fetch_assoc($result_merge_stats);

$sumArray = array();

foreach (mysql_fetch_assoc($result_merge_stats) as $k=>$subArray) {
  foreach ($subArray as $id=>$value) {
    $sumArray[$id]+=$value;
  }
}


print_r($sumArray);

the output is:

Array ( [__] => 1 [______] => 1 [___] => 1 [_______] => 1 )

Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 )

Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 )

Array ( [__] => 1 [_____] => 2 [_______] => 7 [_____] => 2 )

Array ( [__] => 1 [______] => 1 [______] => 1 [_____] => 1 ) 

Link to comment
https://forums.phpfreaks.com/topic/277686-easy-way-to-total-array-rows/
Share on other sites

  On 5/6/2013 at 10:02 PM, Phear46 said:

can you give some sample data and what the expected result from your sample would be. I have an idea but if its my idea its really simple and im proably misunderstanding!

 

 

Better yet, you should provide some details about the DB schema and the query you are using. It would probably be more efficient to use the database for this than to post-process the DB results.

the output data for these would be:
Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 )
Array ( [__] => 1 [_____] => 2 [_______] => 7 [_____] => 2 )

totals:               2                  3                     8                   3

 

 

I forgot to pull the numbers for the corresponding arrays, but you get the picture.

  On 5/6/2013 at 11:26 PM, Q695 said:

the output data for these would be:

Array ( [__] => 1 [______] => 1 [_____] => 1 [_____] => 1 )

Array ( [__] => 1 [_____] => 2 [_______] => 7 [_____] => 2 )

totals:               2                  3                     8                   3

 

 

I forgot to pull the numbers for the corresponding arrays, but you get the picture.

 

Um, what? How are you relating those fields? If you had taken the time to put at least example names for the array indexes it would make some sense. But, what you have there makes no sense. How does an array field with an index of five underscores get added to the array index in another element of four underscores?

 

Based upon what you were attempting previously

foreach (mysql_fetch_assoc($result_merge_stats) as $k=>$subArray) 
{
    foreach ($subArray as $id=>$value)
    {
        $sumArray[$id]+=$value;
    }
}

You should do this in your query. Example

SELECT SUM(field1) as field1total, SUM(field2) as field2total, SUM(field3) as field3total
FROM tableName
  On 5/6/2013 at 11:37 PM, Psycho said:

How are you relating those fields?

How does an array field with an index of five underscores get added to the array index in another element of four underscores?

The fields are being pulled from a database.

The totals are going top down, not left right, and if it was totaling left, right it would be easy to do, and I would put it on the right side of the array so you could logically draw the conclusion based on how math equations were written when you were a kid.

The underscores are names made generic.

  On 5/7/2013 at 4:21 AM, Q695 said:

The fields are being pulled from a database.

The totals are going top down, not left right, and if it was totaling left, right it would be easy to do, and I would put it on the right side of the array so you could logically draw the conclusion based on how math equations were written when you were a kid.

The underscores are names made generic.

 

I think that you are confused about what cyberRobot said. You can select the column you want, send it through a loop and use array_count_values() or count() to count all of the values in the column. Only loop the data in that column.  

  On 5/7/2013 at 4:21 AM, Q695 said:

The fields are being pulled from a database.

The totals are going top down, not left right, and if it was totaling left, right it would be easy to do, and I would put it on the right side of the array so you could logically draw the conclusion based on how math equations were written when you were a kid.

The underscores are names made generic.

 

You obviously don't have an understanding of what the SUM() function in a query does - it sums the values across records (i.e. what you mean by top-down). The whole underscores only makes your example harder to understand since ____ and _____ aren't more different.

 

If you had even looked at the sample query I provided and tried to use it I'm sure you would find it gives you the results you want in one simple query with no need to process the results.

@Q695,

 

I think it would help a great deal to show a true representative set of sample data and what you are trying to achieve. As I stated a long time back in this thread, I am almost certain you can get the data you need without doing any post processing of the data.

 

If you need to sum the columns, then you would run a query such as the one I provided previously:

 

SELECT SUM(field1) as field1total, SUM(field2) as field2total, SUM(field3) as field3total
FROM tableName

 

Or if you need a sum of multiple fields for each records (i.e. sum the row), then you would do something such as Zane provided

 

SELECT (field1+field2+field3) as recordTotal
FROM tableName

He probably wants to show the data also which is why he isn't using the SUM() in the query. He's basically trying to create a totals row at a bottom of a table. My guess is the way you are loop through the query.

 

//this returns only the first result

 

foreach (mysql_fetch_assoc($result_merge_stats) as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}

 

//this is what I'm guessing you want to do

 

while($row = mysql_fetch_assoc($result_merge_stats))
{
    foreach($row as $id => $value)
    {
        $sumArray[$id]+=$value;   
    }
}
 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.