What are the best practices for iterating through arrays in PHP using foreach loops to avoid errors in checking for duplicate values?

When iterating through arrays in PHP using foreach loops, it is important to avoid errors in checking for duplicate values by using associative arrays to keep track of unique values. This can be achieved by creating a new array where the keys are the values of the original array, ensuring that each value is unique. By checking if the value already exists as a key in the associative array, we can easily identify and handle duplicates.

$originalArray = [1, 2, 3, 4, 2, 5, 3];
$uniqueValues = [];

foreach ($originalArray as $value) {
    if (!array_key_exists($value, $uniqueValues)) {
        $uniqueValues[$value] = true;
        // Process the unique value here
        echo $value . " is a unique value.\n";
    } else {
        // Handle duplicate value
        echo $value . " is a duplicate value.\n";
    }
}