php collapse common columns in associative array

Solutions on MaxInterview for php collapse common columns in associative array by the best coders in the world

showing results for - "php collapse common columns in associative array"
Leon
18 Feb 2017
1/* Collapse on common columns which is also the new key e.g.
2([0] => [[id] => 1234, [firstName] => "foo", [1] => [[id] => 1234, [lastName] => "bar")
3will become ([1234] => [[firstName] => "foo", [lastName]="bar"]
4Credit to https://stackoverflow.com/users/762073/xdazz
5*/
6$result = array();
7foreach ($data as $element) {
8    $result[$element['id']][] = $element;// [] appends to the array if it already exists
9}