0

I have an array in PHP of this type, resulting from a particular query on a db.

$output = Array
(
  [0] => Array 
      (
       'price' => 100
       'date' => '2015-07-28'
       'total' => 200
       'qty' => 2
      )
  [1] => Array
      (
       'price' => 80
       'date' => '2015-07-28'
       'total' => 240
       'qty' => 3
      )
  [2] => Array
      (
       'price' => 100
       'date' => '2015-07-29'
       'total' => 300
       'qty' => 3
      )
  [3] => Array
      (
       'price' => 90
       'date' => '2015-07-28'
       'total' => 90
       'qty' => 1
      )
)

I'm trying to sum total and qty based on price key values, obtaining an array that will look like this:

$grouped = Array
(
[0] => Array 
    (
     [price] => 100
     [sumtotal] => 500
     [sumqty] => 5
    )
 [1] => Array 
    (
     [price] => 80
     [sumtotal] => 240
     [sumqty] => 3
    )
 [2] => Array 
    (
     [price] => 90
     [sumtotal] => 90
     [sumqty] => 1
    )
 )

Still cannot find a way to get around this.

AntR
  • 11

3 Answers3

2

Try using simple foreach loop as

$result = array();
foreach ($output as $key => $value) {
    $hash = $value['price'];
    if (isset($result[$hash])) {
        $result[$hash]['price'] = $value['price'];
        $result[$hash]['sumtotal'] += $value['total'];
        $result[$hash]['sumqty'] += $value['qty'];
    } else {
        $result[$hash]['price'] = $value['price'];
        $result[$hash]['sumtotal'] = $value['total'];
        $result[$hash]['sumqty'] = $value['qty'];
    }
}
print_r(array_values($result));

Demo

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
0

I don't understand, it's the same array without the date.. ?

Edit :

Ok try this

    $temp= array();
for($i=0; $i<sizeof($outpout);$i++)
{

if(!isset($temp[$i]))
    $temp[$i]=array();
$temp[$i][$outpout[$i]['price']]+=$outpout[$i]['qty']];
}

$res=array();
$count=0;
foreach($temp as $key => $value)
{
    $res[$count]['price']=$key;
    $res[$count]['qty']=$value;
    $res[$count]['total']=$key*$value;
    $count++;
}
Alexis
  • 5,681
  • 1
  • 27
  • 44
  • No. As you can see i have two elements with price = 100, so i'm just summing up qty and total for this two elements. – AntR Jul 30 '15 at 08:32
0

Okay, here's a simple complete code to solve your problem.

$output=array();//this is your array
$price_index=array();
$final_array=array();
foreach($output as $key=>$value)
{
    //first check if price is already in $price_index
    if(!isset($price_index[$value["price"]]))
    {
        $price_index[$value["price"]]=sizeof($price_index);
    }
    $final_index=$price_index[$value["price"]];
    if(isset($final_array[$final_index]))
    {
        $final_array[$final_index]["total"]+=$value["total"];
        $final_array[$final_index]["qty"]+=$value["qty"];
    }
    else
    {
        $final_array[$final_index]["total"]=$value["total"];
        $final_array[$final_index]["qty"]=$value["qty"];
        $final_array[$final_index]["price"]=$value["price"];
    }
}
//display our final array
print_r($final_array);

What I did:

  • iterate through your array
  • check if the current price has been added to $final_array before, by creating $price_index array that contains price as key and the value is the index of $output array on the first time the price occurred.
  • now if the price occur again, the $price_index[current price] will be detected that it is already been set before, then we will use the value as the index of our $final_array and add the current total and qty to existing.
  • then display the array (optional).

Hope it helps. Please don't just copy my code, analyze and understand it mate :)

VMcreator
  • 833
  • 8
  • 17