Sure, just setup a data structure to hold your results while you process the input, like this:
while (whatever) {
if (isset($results[$age])) {
$results[$age] .= ",$name";
} else {
$results[$age] = $name;
}
}
When your done, $results will be an array indexed by age with the names formatted as in your example. You can then use a simple foreach to print it out:
foreach (array_keys($results) as $age) {
echo $age . $results[$age] ."\n";
}
That snippet should print out just like your example desired output.
NOTE: The above code is merely an example. Please customize for you what need and then debug appropriately.