How can the concept of "grouping breaks" be applied to the problem of avoiding duplicate outputs in a PHP foreach loop?

When iterating over a collection using a PHP foreach loop, duplicate outputs may occur if the data being processed is not unique. To avoid this, we can implement the concept of "grouping breaks" by keeping track of the unique values already processed and skipping duplicates. This can be achieved by storing processed values in an array and checking against it before outputting each item.

$collection = [1, 2, 2, 3, 4, 4, 5];
$processed = [];

foreach ($collection as $item) {
    if (!in_array($item, $processed)) {
        echo $item . "\n";
        $processed[] = $item;
    }
}