PHP Group By Array Key

I search for this more than I thought I would.

As the title suggests, the following allows you to group items of an array by its key.

$new_arr = array();

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

ksort($new_arr, SORT_NUMERIC);

By Tim Cooper

Leave a Comment