stef
stef

Reputation: 27789

Group rows of a 2d array by a column

In the multidimensional array below, I would like to merge arrays that have the same merge_id. I'm not sure "merge" is the right word: in the example below, array['0'] should become array['0'] with in it array['0']['0'] and array['0']['1'], the latter being equal to array['1']. I hope this makes sense ...

The array comes out of the db sorted on merge_id so arrays with matching merge_id are always "next to" each other, and there will only ever be 2 with the same merge_id

As I loop through the array I know I need to keep a variable that is always equal to the previous merge_id and if there is a match between previous and current, then merge.

Array
(
    [0] => Array
        (
            [client_id] => 5
            [company_name] => company111_name
            [id] => 3
            [fee] => 111
            [year] => 2009
            [quarter] => 3
            [date_inserted] => 1264948583
            [description] => 2009 - Q3


            [fee_type] => 
            [merge_id] => a87ff679a2f3e71d9181a67b7542122c
            [total_paid] => 0
            [total_remainder] => 0
        )

    [1] => Array
        (
            [client_id] => 5
            [company_name] => company111_name
            [id] => 6
            [fee] => 55.5
            [year] => 2010
            [quarter] => 2
            [date_inserted] => 1264949470
            [description] => 2010 - Q2


            [fee_type] => 
            [merge_id] => a87ff679a2f3e71d9181a67b7542122c
            [total_paid] => 0
            [total_remainder] => 0
        )

    [2] => Array
        (
            [client_id] => 5
            [company_name] => company111_name
            [id] => 4
            [fee] => 111
            [year] => 2009
            [quarter] => 4
            [date_inserted] => 1264948583
            [description] => 2009 - Q4


            [fee_type] => 
            [merge_id] => 
            [total_paid] => 0
            [total_remainder] => 0
        )

    

    [3] => Array
        (
            [client_id] => 5
            [company_name] => company111_name
            [id] => 7
            [fee] => 55.5
            [year] => 2010
            [quarter] => 3
            [date_inserted] => 1264949470
            [description] => 2010 - Q3


            [fee_type] => 
            [merge_id] => 
            [total_paid] => 0
            [total_remainder] => 0
        )

)

Code

$merger = $data['search']['0']['merge_id'];
$i = 0;
foreach ($data['search'] as $fee) {
    if ($fee['merge_id'] == $merger) {
        //bump up & merge arrays
        ???
    }
    $merger = $fee['merge_id'];
    $i++;
}

Upvotes: 0

Views: 269

Answers (2)

user187291
user187291

Reputation: 53960

  foreach($array as $p)
      $result[$p['merge_id']][] = $p;

Upvotes: 1

Gumbo
Gumbo

Reputation: 655795

You can use the ID as key to put all items with the same ID in the same array:

$merged = array();
foreach ($data['search'] as $fee) {
    if ($fee['merge_id'] == '') {
        continue;
    }
    if (!isset($merged[$fee['merge_id']])) {
        $merged[$fee['merge_id']] = array();
    }
    $merged[$fee['merge_id']][] = $fee;
}
$merged = array_values($merged);

Notice that this will skip the items with an empty merge ID. You could also use a default merge ID in that case by replacing continue; with $fee['merge_id'] = 0;.

Upvotes: 4

Related Questions