$items = []; foreach($product_filters as $product_filter) { $items[$product_filter["id"]][] = $product_filter; } foreach($items as $group) { echo " <tr><td class='description-left'> <span>{$group[0]["name"]}</span> </td> <td class='description-right'>"; foreach($group as $filter) { echo "{$filter['group']}, "; } echo "</td></tr>"; } It turns out that a comma is added to all elements at the end, so that it is not added to the last element
Result code
Заголовок 1 Текст 1, Текст 2, Заголовок 2 Текст 3, Текст 4, Заголовок 3 Текст 5, A must
Заголовок 1 Текст 1, Текст 2 Заголовок 2 Текст 3, Текст 4 Заголовок 3 Текст 5
joinfunction instead of a loop - Grundy