-2

I'm facing a problem with multiple array calculation. I am using database mysql and operation language is PHP. When I'm fetching an result from database result show multiple array with fixed key and value. When one array key code value is same as separate array key code value then this array combine to one with sum quantities and prices. This is fetching result from database.

['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00],
['code'=>'bbb','name'=>'b','qty'=>20,'price'=>12.00],
['code'=>'ccc','name'=>'c','qty'=>20,'price'=>12.00],
['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00],
['code'=>'ddd','name'=>'d','qty'=>20,'price'=>12.00],
['code'=>'ccc','name'=>'c','qty'=>5,'price'=>8.00],
['code'=>'bbb','name'=>'b','qty'=>15,'price'=>10.00],
['code'=>'ggg','name'=>'g','qty'=>20,'price'=>12.00],

and I want to customize this result this format.

['code'=>'aaa','name'=>'a','qty'=>40,'price'=>24.00],
['code'=>'bbb','name'=>'b','qty'=>35,'price'=>22.00],
['code'=>'ccc','name'=>'c','qty'=>25,'price'=>20.00],
['code'=>'ddd','name'=>'d','qty'=>20,'price'=>12.00],
['code'=>'ggg','name'=>'g','qty'=>20,'price'=>12.00],

Please help me to solve this problem . I tried a lot but didn't get any solution.

Abdul Hoque Nuri
  • 1,105
  • 1
  • 9
  • 18
raihan
  • 2,225
  • 4
  • 14
  • 19

4 Answers4

3

You can use array_reduce to give you the result you want. The anonymous callback checks to see if the code from the current value exists in the result array, and if it does, sums the current quantity and price. If it doesn't, the current value is pushed into the result array. The code initialises the result with the first value in the original array so that we don't have to worry about undefined indexes:

$newdata = array_reduce(array_slice($data, 1),
    function ($c, $v) { 
        if (($k = array_search($v['code'], array_column($c, 'code'))) !== false) {
            $c[$k]['qty'] += $v['qty'];
            $c[$k]['price'] += $v['price'];
        }
        else {
            $c[] = $v;
        }
        return $c;
    }, array($data[0]));
print_r($newdata);

Output:

Array (
    [0] => Array ( [code] => aaa [name] => a [qty] => 40 [price] => 24 )
    [1] => Array ( [code] => bbb [name] => b [qty] => 35 [price] => 22 )
    [2] => Array ( [code] => ccc [name] => c [qty] => 25 [price] => 20 )
    [3] => Array ( [code] => ddd [name] => d [qty] => 20 [price] => 12 )
    [4] => Array ( [code] => ggg [name] => g [qty] => 20 [price] => 12 ) 
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • thanks for your answer... its really helpful answer. Little bit complex but helpful @Nick – raihan Dec 27 '18 at 09:07
  • @raihan no worries. Note the difference between this answer and the accepted one is that the output array is numerically indexed, rahter than having indexes of `aaa`, `bbb`, etc. However you can get that result from the accepted answer by using `array_values`. – Nick Dec 27 '18 at 10:09
  • thanks. I got your point . Facing some problem from upper answer but your answer is perfect. – raihan Dec 27 '18 at 10:47
2

you can achieve by single dimension array...

$newArray = array();   //create new array..

foreach($array as $item) {
    if(isset($newArray[$item['code']])) { //if already exist then sum of qty and price...
        $newArray[$item['code']]['qty'] += $newArray[$item['code']]['qty'];
        $newArray[$item['code']]['price'] += $newArray[$item['code']]['price'];
    }else {   //if first time then add array into them...
        $newArray[$item['code']] = $item;
    }
}

by using query you can do like this

SELECT code, name, SUM(qty), SUM(price) 
from tableName 
group by code
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
  • 2
    You always answer questions with *try this one*. That is not a proper answer. A answer should explain why you do something. A lot of your answers have follow up questions from OP asking why it works and you don't even bother to answer them. That is not what SO is about. – Andreas Dec 27 '18 at 07:21
  • @Andreas ok from this time i give you explanation with answer ..once again thanks – Jignesh Joisar Dec 27 '18 at 07:24
1
SELECT code, name, SUM(qty), price 
from your_table_name 
group by name

use group by while fetching result as you are using mysql. Hopefully, this will help.

LogicalAnt
  • 907
  • 3
  • 12
  • 28
0

Try This one

    <?php 

error_reporting(0);
$array=array(['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00],
['code'=>'bbb','name'=>'b','qty'=>20,'price'=>12.00],
['code'=>'ccc','name'=>'c','qty'=>20,'price'=>12.00],
['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00],
['code'=>'ddd','name'=>'d','qty'=>20,'price'=>12.00],
['code'=>'ccc','name'=>'c','qty'=>5,'price'=>8.00],
['code'=>'bbb','name'=>'b','qty'=>15,'price'=>10.00],
['code'=>'ggg','name'=>'g','qty'=>20,'price'=>12.00]);


foreach($array as $item) {
        $newArray[$item['code']]['code'] = $item['code'];
        $newArray[$item['code']]['name'] = $item['name'];
        $newArray[$item['code']]['qty'] += $item['qty'];
        $newArray[$item['code']]['price'] +=$item['price'];
}
$newArray=array_values($newArray);

print_r($newArray);

?>
Arun Kumar
  • 208
  • 1
  • 3
  • 14
  • 1
    Error reporting off is always a good way to start an answer. *Facepalm*. Do you expect there to be errors you need to suppress in your answer? Why not just go the extra few inches and produce a code that you know works? And read the comment I wrote to Jignesh – Andreas Dec 27 '18 at 07:36