//your db rows
$rows = [
['id' => 1, 'name' => 'abc', 'value' => 12],
['id' => 2, 'name' => 'xyz', 'value' =>32],
['id' => 3, 'name' => 'abc', 'value' =>43],
['id' => 4, 'name' => 'cde', 'value' =>54],
['id' => 5, 'name' => 'fgh', 'value' =>75],
['id' => 6, 'name' => 'abc', 'value' =>56],
['id' => 7, 'name' => 'fgh', 'value' =>98],
];
$data = [];
///select same names to array
foreach ($rows as $row){
if(array_key_exists($row['name'], $data)){
array_push($data[$row['name']], $row['value']);
}else{
$data[$row['name']] = [$row['value']];
}
}
//output result
foreach ($data as $key => $value){
if(count($data[$key]) === 1){
echo $key .' = '. $value[0];
echo '<br>';
}else{
foreach ($value as $k => $v){
$k++;
echo $key .'('.$k.') = '. $v;
echo '<br>';
}
}
}
output
abc(0) = 12
abc(1) = 43
abc(2) = 56
xyz = 32
cde = 54
fgh(0) = 75
fgh(1) = 98