1

I have this array:

array(
    array('id' => 1, 'price' => 20.00, 'stock' => 2, 'year' => 11 ),
    array('id' => 1, 'price' => 20.00, 'stock' => 3, 'year' => 12 ),
    array('id' => 1, 'price' => 20.00, 'stock' => 2, 'year' => 13 ),
    array('id' => 2, 'price' => 36.00, 'stock' => 11, 'year' => 13 ),
);

I need to sum the "stock" column when the id is the same (is the same article) and the years are 12 AND 13.

The result should be:

array(
    array('id' => 1, 'price' => 20.00, 'stock' => 2, 'year' => 11 ),
    array('id' => 1, 'price' => 20.00, 'stock' => 5, 'year' => 13 ),
    array('id' => 2, 'price' => 36.00, 'stock' => 11, 'year' => 13 ),
);

Notice the 'year' => 12 has been removed and the stock of that row has been added to the 13 row.

I can't do this without creating a big mess of spaguetti code with a bunch of bucles. Maybe there is a php function to this in a cleaner way?

Sorry for my english.

2
  • 2
    A PHP function that groups certain rows of a multidimensional array (specified by some arbitrary condition), sums a specific column and assumes that the non-grouped, non-summed columns have the same value (because otherwise the grouping would not make sense)? No. But SQL does that. Perhaps you should look there for the solution? Commented Nov 18, 2013 at 13:07
  • It's a good idea, but how can i group by year when its 12 or 13? not always Commented Nov 18, 2013 at 13:26

2 Answers 2

3

If you insist on solving this issue using PHP instead of your database, this will do the trick:

function group_sum($array) 
{
    $return = array();

    foreach ($array as $v)
    {
        $key = $v['id'] . '_' . ($v['year'] == 12 ? 13 : $v['year']);

        if (array_key_exists($key, $return))
        {
            $return[$key]['stock'] += $v['stock'];

            if ($v['year'] > $return[$key]['year'])
                $return[$key]['year'] = $v['year'];
        }
        else
        {
            $return[$key] = $v;
        }
    }

    return $return;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I try and reach upto here...Please anyone can push this code little...

$ary = array(
  array('id' => 1, 'price' => 20.00, 'stock' => 8, 'year' => 11 ),
  array('id' => 1, 'price' => 20.00, 'stock' => 3, 'year' => 12 ),
  array('id' => 1, 'price' => 20.00, 'stock' => 2, 'year' => 13 ),
  array('id' => 2, 'price' => 36.00, 'stock' => 11, 'year' => 13 ),
);

$ary2 = $ary;
$i=0;
foreach($ary as $row){

foreach($ary2 as $row2){
    if($row['id']==$row2['id'] && $row['year'] > $row2['year']){
        $ary[$i]['stock'] = $row['stock'] + $row2['stock'];
        unset($row);
    }   
}
//
$i++;
}
echo "<pre/>";
print_r($ary);

Comments

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.