106

I have the following array

Array
(
    [0] => Array
        (
            [id] => 96
            [shipping_no] => 212755-1
            [part_no] => reterty
            [description] => tyrfyt
            [packaging_type] => PC
        )

    [1] => Array
        (
            [id] => 96
            [shipping_no] => 212755-1
            [part_no] => dftgtryh
            [description] => dfhgfyh
            [packaging_type] => PC
        )

    [2] => Array
        (
            [id] => 97
            [shipping_no] => 212755-2
            [part_no] => ZeoDark
            [description] => s%c%s%c%s
            [packaging_type] => PC
        )

)

How can I group the array by id? Is there any native php functions are available to do this?

While this approach works, I want to do this using a foreach, since with the above I will get duplicate items, which I'm trying to avoid?

On the above example id have 2 items, so its need to be inside of the id

Braiam
  • 1
  • 11
  • 47
  • 78
Red
  • 6,230
  • 12
  • 65
  • 112

21 Answers21

212

There is no native one, just use a loop.

$result = array();
foreach ($data as $element) {
    $result[$element['id']][] = $element;
}
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • how does this know the similar ids? – fiberOptics Mar 07 '19 at 10:49
  • BAKARI see these relevant posts: https://stackoverflow.com/a/47926978/2943403 and https://stackoverflow.com/a/61968973/2943403 and https://stackoverflow.com/a/52485161/2943403 – mickmackusa Jun 13 '20 at 16:42
  • 1
    You forgot to `ksort($result, SORT_NUMERIC);` in the end. Question shows sorted keys – Erdal G. Jul 17 '20 at 12:26
  • @fiberOptics The result array unconditionally pushes newly indexed elements into each group (grouped explicitly by `$element['id']`. This question could use a bit more explanation for researchers that are inexperienced with this technique/language. – mickmackusa May 18 '22 at 01:14
46

In a more functional programming style, you could use array_reduce

$groupedById = array_reduce($data, function (array $accumulator, array $element) {
  $accumulator[$element['id']][] = $element;

  return $accumulator;
}, []);
thiagobraga
  • 1,519
  • 2
  • 15
  • 29
atomrc
  • 2,543
  • 1
  • 16
  • 20
43

You can try the following:

$group = array();

foreach ( $array as $value ) {
    $group[$value['id']][] = $value;
}

var_dump($group);

Output:

array
  96 => 
    array
      0 => 
        array
          'id' => int 96
          'shipping_no' => string '212755-1' (length=8)
          'part_no' => string 'reterty' (length=7)
          'description' => string 'tyrfyt' (length=6)
          'packaging_type' => string 'PC' (length=2)
      1 => 
        array
          'id' => int 96
          'shipping_no' => string '212755-1' (length=8)
          'part_no' => string 'dftgtryh' (length=8)
          'description' => string 'dfhgfyh' (length=7)
          'packaging_type' => string 'PC' (length=2)
  97 => 
    array
      0 => 
        array
          'id' => int 97
          'shipping_no' => string '212755-2' (length=8)
          'part_no' => string 'ZeoDark' (length=7)
          'description' => string 's%c%s%c%s' (length=9)
          'packaging_type' => string 'PC' (length=2)
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Baba
  • 94,024
  • 28
  • 166
  • 217
18

I just threw this together, inspired by .NET LINQ

<?php

// callable type hint may be "closure" type hint instead, depending on php version
function array_group_by(array $arr, callable $key_selector) {
  $result = array();
  foreach ($arr as $i) {
    $key = call_user_func($key_selector, $i);
    $result[$key][] = $i;
  }  
  return $result;
}

 $data = array(
        array(1, "Andy", "PHP"),
        array(1, "Andy", "C#"),
        array(2, "Josh", "C#"),
        array(2, "Josh", "ASP"),
        array(1, "Andy", "SQL"),
        array(3, "Steve", "SQL"),
    );

$grouped = array_group_by($data, function($i){  return $i[0]; });

var_dump($grouped);

?>

And voila you get

array(3) {
  [1]=>
  array(3) {
    [0]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      string(4) "Andy"
      [2]=>
      string(3) "PHP"
    }
    [1]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      string(4) "Andy"
      [2]=>
      string(2) "C#"
    }
    [2]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      string(4) "Andy"
      [2]=>
      string(3) "SQL"
    }
  }
  [2]=>
  array(2) {
    [0]=>
    array(3) {
      [0]=>
      int(2)
      [1]=>
      string(4) "Josh"
      [2]=>
      string(2) "C#"
    }
    [1]=>
    array(3) {
      [0]=>
      int(2)
      [1]=>
      string(4) "Josh"
      [2]=>
      string(3) "ASP"
    }
  }
  [3]=>
  array(1) {
    [0]=>
    array(3) {
      [0]=>
      int(3)
      [1]=>
      string(5) "Steve"
      [2]=>
      string(3) "SQL"
    }
  }
}
AndyClaw
  • 740
  • 8
  • 15
8

Consume and cache the column value that you want to group by, then push the remaining data as a new subarray of the group you have created in the the result.

function array_group(array $data, $by_column)
{
    $result = [];
    foreach ($data as $item) {
        $column = $item[$by_column];
        unset($item[$by_column]);
        $result[$column][] = $item;
    }
    return $result;
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • This correct answer differs from others on this page because it removes the grouping column from subarrays pushed into each group. – mickmackusa Mar 16 '23 at 04:11
5

If you desire a Composer alternative with a full suite of tests, the array_group_by function achieves what you are looking for. Full disclosure: I am the author of said library.

$grouped = array_group_by($arr, 'id');

It also supports multi-level groupings, or even complex grouping through use of custom callback functions:

// Multilevel grouping
$grouped = array_group_by($arr, 'id', 'part_no');

// Grouping by a callback/callable function
$grouped = array_group_by($records, function ($row) {
    return $row->city;
});
Jake Z
  • 1,520
  • 1
  • 17
  • 26
  • Warning: This method `array_group_by` introduce the variable `$key` as a method parameter and overridden in the `foreach` – DannyFeliz May 16 '17 at 16:18
  • 1
    If that github link every dies or moves, this answer will be rendered useless. Your working solution should be fully/statically written in your answer. If you are the owner of the code being referenced, you are expected to make explicit attribution. Please [edit] your answer. – mickmackusa Aug 09 '21 at 03:09
  • In your custom function, `$groupKey = null;` is useless because it is unconditionally overwritten. `elseif` should be one word in PHP. Does `func_num_args()` really need to be called more than once? – mickmackusa Aug 09 '21 at 03:14
  • @mickmackusa I have updated the answer to reflect ownership of the linked library. This is a mistake that I admit. To your other points, I disagree that a full written example is necessary. While possible, it is not practical to copy 50 lines of code into Stack Overflow when the entire point is to provide a library/Composer alternative to untested and limited code. Composer was a pivotal moment for PHP, and without it PHP would be a maintenance nightmare for sufficiently large applications. – Jake Z Aug 09 '21 at 04:21
  • @mickmackusa Per the `$groupKey` comment, it is not useless. It is good practice to initialize variables even if PHP does not make it necessary. PHP's own manual states as much. Further, in many languages, variables created in nested statements are not available outside of that nesting. Yes, `func_num_args` need not technically be called if the length is passed through recursive calls and is subtracted on each nesting, but that micro optimization results in less readable code for very few gains. – Jake Z Aug 09 '21 at 04:23
  • "_ It is good practice to initialize variables even if PHP does not make it necessary._" Yes, if it is possible that it will not be declared before it is access. In this case, you are _unconditionally_ overwriting the `null` value -- see how that is a wasted action? It is good practice to store a value in a variable when you will use it more than once. When you already have a 50line function why claim that you are fighting against bloat when you are making redundant function calls? Can your library make use the splat operator to modernize the script? – mickmackusa Aug 09 '21 at 04:26
  • Stack Overflow will show your script to a limited height if you post it here. This means that researchers won't need to "link chase" to see the code behind your suggestion. – mickmackusa Aug 09 '21 at 04:31
  • It is good practice whether it is overwritten by nested code or not. Again, many other languages (e.g. JavaScript and Python) require a variable to be initialized, even if it will be immediately overwritten by nested code. That PHP supports uninitialized variables is irrelevant when we want portable code that conforms to best practices. If the conditionals were just an `if/else`, a ternary operator could be used, but that is not the case here. A couple lines in an *imported library* is hardly bloat. You again miss the main reason: maintainability and portability. – Jake Z Aug 09 '21 at 04:35
  • This is polluting the comments and I request that this unproductive discussion stop. We clearly disagree. I do not think someone posting the library of `react-select` in its entirety to a Stack Overflow question about the proper React component for select lists is desirable, for example. – Jake Z Aug 09 '21 at 04:38
  • @DannyFeliz What does it mean? – Alex78191 Oct 13 '21 at 06:19
4

$arr = Data Araay;

$fldName = Group By Colum Name;

function array_group_by( $arr, $fldName) {
    $groups = array();
    foreach ($arr as $rec) {
        $groups[$rec[$fldName]] = $rec;
    }
    return $groups;
}

function object_group_by( $obj, $fldName) {
    $groups = array();
    foreach ($obj as $rec) {
        $groups[$rec->$fldName] = $rec;
    }
    return $groups;
}
Mahidul Islam
  • 580
  • 11
  • 29
  • 1
    I think your code is missing `[]` for actual pushing of items inside groups; `$groups[$rec[$fldName]][] = $rec;` insteald of `$groups[$rec[$fldName]] = $rec;`. same for object implementation. – Jacer Omri Jul 23 '22 at 01:57
2
$arr = array();

foreach($old_arr as $key => $item)
{
   $arr[$item['id']][$key] = $item;
}

ksort($arr, SORT_NUMERIC);
Sebass van Boxel
  • 2,514
  • 1
  • 22
  • 37
  • Kindly,__First i need to manipulate the array ? and then again foreach for the real purpose ? – Red Oct 03 '12 at 10:19
2
for($i = 0 ; $i < count($arr)  ; $i++ )
{
    $tmpArr[$arr[$i]['id']] = $arr[$i]['id'];
}
$vmpArr = array_keys($tmpArr);
print_r($vmpArr);
  • This answer is very wrong -- it is only returning the keys and will kill duplicates. It is also using the poor practice of calling `count()` on every iteration. Code-only answers are low-value on SO. I might never understand upvoters. – mickmackusa Jun 13 '20 at 16:46
2

Expanding on @baba's answer, which I like, but creates a more complex three level deep multi-dimensional (array(array(array))):

$group = array();
 foreach ( $array as $value ) {
   $group[$value['id']][] = $value; 
 }

// output only data from id 96
foreach ($group as $key=>$value) { //outer loop
 foreach ($value as $k=>$v){ //inner loop
  if($key==96){ //if outer loop is equal to 96 (could be variable)
   for ($i=0;$i<count($k);$i++){ //iterate over the inner loop
        printf($key.' has a part no. of '.$v['part_no'].' and shipping no. of '.$v['shipping_no'].'<br>');
   }
 }
}
 }

Will output:

96 has a part no. of reterty and shipping number of 212755-1

96 has a part no. of dftgtryh and shipping number of 212755-1

rwhite
  • 430
  • 2
  • 11
2

It's trivial to do with LINQ, which is implemented in PHP in several libraries, including YaLinqo*. It allows performing SQL-like queries on arrays and objects. The groubBy function is designed specifically for grouping, you just need to specify the field you want to group by:

$grouped_array = from($array)->groupBy('$v["id"]')->toArray();

Where '$v["id"]' is a shorthand for function ($v) { return $v["id"]; } which this library supports.

The result will be exactly like in the accepted answer, just with less code.

* developed by me

Athari
  • 33,702
  • 16
  • 105
  • 146
  • Can you give me a hint how to group by multiple fields? In my use case I have an object with a date field and need to group it by year, month and date. Something like { 2016 => { 09 => { 15 => $object } } } – fnagel Sep 16 '16 at 22:37
  • 1
    @fnagel Nested grouping is somewhat complicated. See the [support issue about nested groupBy](https://github.com/Athari/YaLinqo/issues/19). It includes a helper function `group_by_multiple`, as well as a lenghty explanation of how nested grouping works. With this function, your query will look like `group_by_multiple($objects, [ '$v->date->format("Y")', '$v->date->format("n")', '$v->date->format("j")' ])`. – Athari Sep 17 '16 at 14:14
2

1. GROUP BY one key

This function works as GROUP BY for array, but with one important limitation: Only one grouping "column" ($identifier) is possible.

function arrayUniqueByIdentifier(array $array, string $identifier)
{
    $ids = array_column($array, $identifier);
    $ids = array_unique($ids);
    $array = array_filter($array,
        function ($key, $value) use($ids) {
            return in_array($value, array_keys($ids));
        }, ARRAY_FILTER_USE_BOTH);
    return $array;
}

2. Detecting the unique rows for a table (twodimensional array)

This function is for filtering "rows". If we say, a twodimensional array is a table, then its each element is a row. So, we can remove the duplicated rows with this function. Two rows (elements of the first dimension) are equal, if all their columns (elements of the second dimension) are equal. To the comparsion of "column" values applies: If a value is of a simple type, the value itself will be use on comparing; otherwise its type (array, object, resource, unknown type) will be used.

The strategy is simple: Make from the original array a shallow array, where the elements are imploded "columns" of the original array; then apply array_unique(...) on it; and as last use the detected IDs for filtering of the original array.

function arrayUniqueByRow(array $table = [], string $implodeSeparator)
{
    $elementStrings = [];
    foreach ($table as $row) {
        // To avoid notices like "Array to string conversion".
        $elementPreparedForImplode = array_map(
            function ($field) {
                $valueType = gettype($field);
                $simpleTypes = ['boolean', 'integer', 'double', 'float', 'string', 'NULL'];
                $field = in_array($valueType, $simpleTypes) ? $field : $valueType;
                return $field;
            }, $row
        );
        $elementStrings[] = implode($implodeSeparator, $elementPreparedForImplode);
    }
    $elementStringsUnique = array_unique($elementStrings);
    $table = array_intersect_key($table, $elementStringsUnique);
    return $table;
}

It's also possible to improve the comparing, detecting the "column" value's class, if its type is object.

The $implodeSeparator should be more or less complex, z.B. spl_object_hash($this).


3. Detecting the rows with unique identifier columns for a table (twodimensional array)

This solution relies on the 2nd one. Now the complete "row" doesn't need to be unique. Two "rows" (elements of the first dimension) are equal now, if all relevant "fields" (elements of the second dimension) of the one "row" are equal to the according "fields" (elements with the same key).

The "relevant" "fields" are the "fields" (elements of the second dimension), which have key, that equals to one of the elements of the passed "identifiers".

function arrayUniqueByMultipleIdentifiers(array $table, array $identifiers, string $implodeSeparator = null)
{
    $arrayForMakingUniqueByRow = $removeArrayColumns($table, $identifiers, true);
    $arrayUniqueByRow = $arrayUniqueByRow($arrayForMakingUniqueByRow, $implodeSeparator);
    $arrayUniqueByMultipleIdentifiers = array_intersect_key($table, $arrayUniqueByRow);
    return $arrayUniqueByMultipleIdentifiers;
}

function removeArrayColumns(array $table, array $columnNames, bool $isWhitelist = false)
{
    foreach ($table as $rowKey => $row) {
        if (is_array($row)) {
            if ($isWhitelist) {
                foreach ($row as $fieldName => $fieldValue) {
                    if (!in_array($fieldName, $columnNames)) {
                        unset($table[$rowKey][$fieldName]);
                    }
                }
            } else {
                foreach ($row as $fieldName => $fieldValue) {
                    if (in_array($fieldName, $columnNames)) {
                        unset($table[$rowKey][$fieldName]);
                    }
                }
            }
        }
    }
    return $table;
}
automatix
  • 14,018
  • 26
  • 105
  • 230
1

This should group an associative array Ejm Group By Country

function getGroupedArray($array, $keyFieldsToGroup) {   
    $newArray = array();

    foreach ($array as $record) 
        $newArray = getRecursiveArray($record, $keyFieldsToGroup, $newArray);

    return $newArray;
}
function getRecursiveArray($itemArray, $keys, $newArray) {
    if (count($keys) > 1) 
        $newArray[$itemArray[$keys[0]]] = getRecursiveArray($itemArray,    array_splice($keys, 1), $newArray[$itemArray[$keys[0]]]);
    else
        $newArray[$itemArray[$keys[0]]][] = $itemArray;

    return $newArray;
}

$countries = array(array('Country'=>'USA', 'State'=>'California'),
                   array('Country'=>'USA', 'State'=>'Alabama'),
                   array('Country'=>'BRA', 'State'=>'Sao Paulo'));

$grouped = getGroupedArray($countries, array('Country'));
1

Check indexed function from Nspl:

use function \nspl\a\indexed;
$grouped = indexed($data, 'id');
Ihor Burlachenko
  • 4,689
  • 1
  • 26
  • 25
1
function array_group_by($arr, array $keys) {

if (!is_array($arr)) {
    trigger_error('array_group_by(): The first argument should be an array', E_USER_ERROR);
}
if (count($keys)==0) {
    trigger_error('array_group_by(): The Second argument Array can not be empty', E_USER_ERROR);
}

// Load the new array, splitting by the target key
$grouped = [];
foreach ($arr as $value) {
    $grouped[$value[$keys[0]]][] = $value;
}

// Recursively build a nested grouping if more parameters are supplied
// Each grouped array value is grouped according to the next sequential key
if (count($keys) > 1) {
        foreach ($grouped as $key => $value) {
       $parms = array_merge([$value], [array_slice($keys, 1,count($keys))]);
       $grouped[$key] = call_user_func_array('array_group_by', $parms);

    }
}
return $grouped;

}

1
function groupeByPHP($array,$indexUnique,$assoGroup,$keepInOne){
$retour = array();
$id = $array[0][$indexUnique];
foreach ($keepInOne as $keep){
    $retour[$id][$keep] = $array[0][$keep];
}
foreach ($assoGroup as $cle=>$arrayKey){
    $arrayGrouped = array();
        foreach ($array as $data){
            if($data[$indexUnique] != $id){
                $id = $data[$indexUnique];
                foreach ($keepInOne as $keep){
                    $retour[$id][$keep] = $data[$keep];
                }
            }
            foreach ($arrayKey as $val){
                $arrayGrouped[$val] = $data[$val];
            }
            $retour[$id][$cle][] = $arrayGrouped;
            $retour[$id][$cle] = array_unique($retour[$id][$cle],SORT_REGULAR);
        }
}
return  $retour;
}

Try this function

groupeByPHP($yourArray,'id',array('desc'=>array('part_no','packaging_type')),array('id','shipping_no')) 
derloopkat
  • 6,232
  • 16
  • 38
  • 45
ZeroCool
  • 99
  • 9
1

Recursive function grouping 2-dimensional array by keys from first to last

Input:

$arr = array(
    '0' => array(
        'key0' => 'value0',
        'key1' => 'value1',
        'key2' => 'value02',
    ),
    '2' => array(
        'key0' => 'value0',
        'key1' => 'value1',
        'key2' => 'value12',
    ),
    '3' => array(
        'key0' => 'value0',
        'key1' => 'value3',
        'key2' => 'value22',
    ),
);
$keys = array('key0', 'key1', 'key2');

Output:

$arr = array(
    'value0' => array(
        'value1 => array(
            'value02' => null,
            'value12' => null,
        ),
        'value3' => 'value22',
    ),
);

Code:

function array_group_by_keys(&$arr, $keys) {

    if (count($arr) < 2){
        $arr = array_shift($arr[0]);
        return;
    }

    foreach ($arr as $k => $item) {
        $fvalue = array_shift($item);
        $arr[$fvalue][] = $item;
        unset($arr[$k]);
    }

    array_shift($keys);
    foreach ($arr as &$sub_arr) {
        array_group_by_keys($sub_arr, $keys);
    }
}
Community
  • 1
  • 1
alsator
  • 121
  • 1
  • 1
  • 5
1

It's easy, you can group by any "key" in the array by using my function groupBy();

$data = [
    [
        "id" => 96,
        "shipping_no" => "212755-1",
        "part_no" => "reterty",
        "description" => "tyrfyt",
        "packaging_type" => "PC"
    ],
    [
        "id" => 96,
        "shipping_no" => "212755-1",
        "part_no" => "dftgtryh",
        "description" => "dfhgfyh",
        "packaging_type" => "PC"
    ],
    [
        "id" => 97,
        "shipping_no" => "212755-2",
        "part_no" => "ZeoDark",
        "description" => "s%c%s%c%s",
        "packaging_type" => "PC"
    ]
];

function groupBy($array, $key) {
    $groupedData = [];
    $data = [];
    $_id = "";
    for ($i=0; $i < count($array); $i++) { 
        $row = $array[$i];
        if($row[$key] != $_id){
            if(count($data) > 0){
                $groupedData[] = $data;
            }
    
            $_id = $row[$key];
            $data = [
                $key => $_id
            ];
        }
    
        unset($row[$key]);
        $data["data"][] = $row;

        if($i == count($array) - 1){
            $groupedData[] = $data;
        }
    }
    
    return $groupedData; 
}

print_r(groupBy($data, "id"));

The results will be:

Array
(
    [0] => Array
        (
            [id] => 96
            [data] => Array
                (
                    [0] => Array
                        (
                            [shipping_no] => 212755-1
                            [part_no] => reterty
                            [description] => tyrfyt
                            [packaging_type] => PC
                        )

                    [1] => Array
                        (
                            [shipping_no] => 212755-1
                            [part_no] => dftgtryh
                            [description] => dfhgfyh
                            [packaging_type] => PC
                        )

                )

        )

    [1] => Array
        (
            [id] => 97
            [data] => Array
                (
                    [0] => Array
                        (
                            [shipping_no] => 212755-2
                            [part_no] => ZeoDark
                            [description] => s%c%s%c%s
                            [packaging_type] => PC
                        )

                )

        )

)

If you change the "key" parameter, it should works without changes:

print_r(groupBy($data, "shipping_no"));

Array
(
    [0] => Array
        (
            [shipping_no] => 212755-1
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 96
                            [part_no] => reterty
                            [description] => tyrfyt
                            [packaging_type] => PC
                        )

                    [1] => Array
                        (
                            [id] => 96
                            [part_no] => dftgtryh
                            [description] => dfhgfyh
                            [packaging_type] => PC
                        )

                )

        )

    [1] => Array
        (
            [shipping_no] => 212755-2
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 97
                            [part_no] => ZeoDark
                            [description] => s%c%s%c%s
                            [packaging_type] => PC
                        )

                )

        )

)
Basem
  • 251
  • 3
  • 9
  • This late answer is missing its educational explanation. Frankly, it looks to convoluted/over-engineered for me to bother understanding the logic/reason for the syntax (there's simply too many variables, conditions, and function calls for such a basic task). – mickmackusa May 18 '22 at 01:15
0

How about multiple level grouping.

data:

$rows = [
    ['country'=>'Japan',  'city'=>'Tokyo', 'surname'=>'Miyazaki', 'name'=>'Hayao'],
    ['country'=>'France', 'city'=>'Paris', 'surname'=>'Godard',   'name'=>'Jean-Luc'],
    ['country'=>'France', 'city'=>'Lyon',  'surname'=>'Godard',   'name'=>'Marguerite'],
    ['country'=>'Japan',  'city'=>'Tokyo', 'surname'=>'Miyazaki', 'name'=>'Akira'],
    ['country'=>'Japan',  'city'=>'Nara',  'surname'=>'Kurosawa', 'name'=>'Akira'],
    ['country'=>'France', 'city'=>'Paris', 'surname'=>'Duras',    'name'=>'Marguerite'],
];
$groups = groupBy($rows, 'country', 'city', 'surname');

code:

    function groupBy($rows, ...$keys)
    {
        if ($key = array_shift($keys)) {
            $groups = array_reduce($rows, function ($groups, $row) use ($key) {
                $group = is_object($row) ? $row->{$key} : $row[$key]; // object is available too.
                $groups[$group][] = $row;
                return $groups;
            }, []);
            if ($keys) {
                foreach ($groups as $subKey=>$subRows) {
                    $groups[$subKey] = self::groupBy($subRows, ...$keys);
                }
            }
        }
        return $groups;
    }
0

What about array_combine() ? Using array_combine() stores each row on the index of $groupByColumn, so we can use that $groupByColumn as keys. This returns the last row for every group (array_combine() overwrites the value when the key already exists - see https://www.php.net/manual/en/function.array-combine.php#111668). If you want to return the first or some specific row, you can play around with array_reverse() or usort() etc.

$result = array_combine(
    array_column($source, $groupByColumn),
    $source
);
Vali
  • 16
  • 1
0

By pushing a reference variable into the result array upon encountering a unique identifying column value, you don't need to re-index your result array after grouping.

Code: (Demo)

$result = [];
foreach ($array as $row) {
    $key = $row['id'];  // assign the grouping column's value
    unset($row['id']);  // remove the grouping column from the row
    if (!isset($ref[$key])) {
        $ref[$key] = [$row];      // declare first entry as a subarray
        $result[] = &$ref[$key];  // push the reference variable into the result array
    } else {
        $ref[$key][] = $row; // add a child to the reference
    }
}
var_export($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136