0

Ive seen many posts explaining to use array_unique but i dont think that will work with my scenario...

I have multiple results in this report, if you notice from my picture the ST/OT has duplicate 1.50 and 2.00 values because each sql entry has 1.50 and 2.00 for each st_ot cell

At the bottom where it totals up, obviously now from $stot_total += $data["stot"];. It is adding up the total. But what I really want is the total of 1.50 + 2.00 (3.50). I dont want it to add the duplicates.

All the ones that are 1.50 have the same ID# as well, so if theres a way to only add values from ID#1, then ID#2, then ID#3 and so on... that would work for me...

i apologize for this horrible post... this question is regarding the ST/OT values only **

foreach($dary as $data){
$stot_total += $data["stot"];
}

enter image description here

1
  • Show content of $dary variable (by var_dump or print_r) Commented Jun 27, 2014 at 15:25

2 Answers 2

1

A simple modification of the answer from @awei should do the trick for you:

<?php
$stot_bucket = array();

foreach($dary as $data)
{
    // Since you don't need to access the values in a multi-dimensional way, just combine the two keys into one to make the final summing simpler
    $stot_bucket[$data["id"] . $data["stot"]] = $data["stot"];
}

$stot_total = array_sum($stot_bucket);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You could try something like this:

<?php
     $stot_bucket = array();
     
     foreach ($records as $record) {
          // do your normal rendering of all the rows
          $stot_bucket[] = $record['stot'];
     }
     
     $stot_total = array_sum(array_unique($stot_bucket));
?>

reference: array_unique docs

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2 i.e. when the string representation is the same, the first element will be used.

2 Comments

this kinda works... but i need it to grab values from unique id# $data["id"]; because if i have more rows below that are 2.00 but from a different ID# i want those 2.00 added aswell to the total
so, just to fully understand, you need to add where the stot is unique with respect to its respective id? do you have an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.