$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 
  • 2
    use the join function instead of a loop - Grundy

1 answer 1

 $numItems = count($group); $i = 0; foreach($group as $filter) { if(++$i === $numItems) echo "{$filter['group']} "; else echo "{$filter['group']}, "; } 
  • one
    everything turned out to be very simple, thanks - Roman Fedorov