How to glue data have the same keys?

Hi there here is a job
There is such data
Andrew 9
Basil 11
Novel 7
Andrew 6
Novel 11
Basil 6

You want to display
Andrew: 9, 6; 15
Vasily: 11, 6
etc.

What options will advise you how to do it?
March 19th 20 at 08:27
2 answers
March 19th 20 at 08:29
Solution
$users = [
 'Andrew, 9',
 'Basil 11',
 'Roman 7',
 'Andrew 6',
 'Roman 11',
 'Basil 6',
];

$temp = [];

foreach ($users as $user) {
 $userData = explode(' ', $user);
 $temp[$userData[0]][] = $userData[1];
}

foreach ($temp as $name => $item) {
 echo sprintf('%s: %s<br/>', $name, implode(',', $item));
}
Thank you - Maudie_Rutherford commented on March 19th 20 at 08:32
March 19th 20 at 08:31
The array keys may not be the same

Find more questions by tags PHP