Answers for "php collapse common columns in associative array"

PHP
0

php collapse common columns in associative array

/* Collapse on common columns which is also the new key e.g.
([0] => [[id] => 1234, [firstName] => "foo", [1] => [[id] => 1234, [lastName] => "bar")
will become ([1234] => [[firstName] => "foo", [lastName]="bar"]
Credit to https://stackoverflow.com/users/762073/xdazz
*/
$result = array();
foreach ($data as $element) {
    $result[$element['id']][] = $element;// [] appends to the array if it already exists
}
Posted by: Guest on November-10-2020

Code answers related to "php collapse common columns in associative array"

Browse Popular Code Answers by Language