- Convert source array to hash table (
key => value
array). - Build desired result array from hash table.
Implementation:
$result = array();
foreach ($array as $el) {
if (!isset($result[$id = $el['id']])) $result[$id] = array();
$result[$id][] = $el['tag'];
}
$result = array_map(
function ($id, $tags) { return array('id' => $id, 'tags' => $tags); },
array_keys($result), array_values($result)
);