get unique value from array and sum totals

不羁的心 提交于 2020-01-25 07:20:08

问题


From this Array() I want to grab the values of amount_total, shipping and partner and total them up by specific partner. So for example the partner=>2665 should have a amount_total of 41.79 + 55.95 and so on please help. I don't want to do it through SQL because I need the data as it is as well.

Array
(
    [0] => Array
    (
        [amount_total] => 41.79
        [amount_shipping] => 4.99
        [amount_partner] => 14.8
        [partner] => 2665
    )
    [1] => Array
    (
        [amount_total] => 55.95
        [amount_shipping] => 19.96
        [amount_partner] => 11
        [partner] => 2665
    )
    [2] => Array
    (
        [amount_total] => 51.96
        [amount_shipping] => 7.98
        [amount_partner] => 23.98
        [partner] => 51754
    )
    [3] => Array
    (
        [amount_total] => 24.55
        [amount_shipping] => 4.99
        [amount_partner] => 5.67
        [partner] => 11513
    )
)

回答1:


You could use PHP to achieve this, but this won't be necessary. The database can do this much more efficiently

I assume your current query looks something like this

SELECT
    `amount_total`,
    `amount_shipping`,
    `amount_partner`,
    `partner`
FROM
    `salesLeads`
WHERE
    [..]

MySQL gives you nice aggregate functions like SUM you can use with GROUP BY

SELECT
    SUM( `amount_total` ) AS `amount_total`,
    SUM( `amount_shipping` ) AS `amount_shipping`,
    SUM( `amount_partner` ) AS `amount_partner`.
    `partner`
FROM 
    `salesLeads`
WHERE
     [..]
GROUP BY
     `partner`

in your PHP script you access this the same way but it has the final numbers grouped and summarized by partner already

e.g.

if ($result = $mysqli->query($query)) {

    while ($row = $result->fetch_assoc()) {
        print_r( $row );
    }

    $result->close();
}

EDIT

Because you wanted a PHP solution, which again is more efficient than querying twice:

$partnerSums = array();
while ( $row = $result->fetch_assoc() ) {
     if ( !array_key_exists( $row['partner'], $partnerSums ) {
          $partnerSums[$row['partner']] = $row;
     } else {
          $partnerSums[$row['partner']]['amount_total'] += $row['amount_total'];
          $partnerSums[$row['partner']]['amount_shipping'] += $row['amount_shipping'];
          $partnerSums[$row['partner']]['amount_partner'] += $row['amount_partner'];
     }
}

print_r( $partnerSums );



回答2:


I think looping through this array and storing the totals in new array, along with checking in_array (more here) should be sufficient. All you need to do is every time you loop through an element, check if it's already in new array, and if it is - perform necessary mathematical operation




回答3:


I would suggest something like this:

   $res['totals']=array();

foreach($result as $res)
{
    $partner = $result['partner'];
    $res['totals'][$partner]+=$result['total_amount'];
}   

This way you get all the data you need in just one place: the partner array '$res'.

Hope it helps you.



来源:https://stackoverflow.com/questions/14060560/get-unique-value-from-array-and-sum-totals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!