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.

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

That's the better way :D
Note that the output of this is partially correct only, because the output array indexes will be 100,80,90 and not 0,1,2
I don't think so did you tried the last line array_values($result) it'll make it 0,1,2 and not 100,80,90 @VMcreator
sorry missed that line... I thought its print_r() only. My bad.
@VMcreator No Problem. Have a great day.
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++;
}

1 Comment

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.
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 :)

1 Comment

The output is exactly like the second array in the question.

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.