What are some strategies for flagging and controlling repeated output of specific data in PHP loops?

When working with PHP loops, sometimes we may encounter the need to flag and control repeated output of specific data. One common strategy is to use a conditional statement within the loop to check if the data has already been outputted, and if so, skip the output. Another approach is to store the data in an array and check if it already exists in the array before outputting it.

// Example of flagging and controlling repeated output of specific data in a PHP loop

$flaggedData = [];

foreach ($dataArray as $data) {
    // Check if the data has already been outputted
    if (!in_array($data, $flaggedData)) {
        // Output the data
        echo $data;

        // Add the data to the flaggedData array
        $flaggedData[] = $data;
    }
}