在Foreach循环中忽略重复项

I have the following script that loops through form entries on my website.

However, I'd like to remove any duplicate entries (in this case, entries using the same email address).

So if my foreach loop finds a duplicate email address, it breaks the loop.

How do I achieve this using my script below?

foreach ($scouts as $participant) {
            $fname    = ucfirst($participant['2']);
            $lname    = ucfirst($participant['3']);
            $email    = $participant['5'];
            $html .= "\t<li><a href=>$fname $lname $email</a></li>
";
    }

Create another array to store email addresses we've already outputted, then on each iteration check we've not used that e-mail address.

$emails = array(); //array to store unique emails (the ones we've already used)
foreach ($scouts as $participant) {
    $fname    = ucfirst($participant['2']);
    $lname    = ucfirst($participant['3']);
    $email    = $participant['5'];
    if( in_array($email, $emails) ) { //If in array, skip iteration
       continue;
    }
    $html .= "\t<li><a href=>$fname $lname $email</a></li>
";
    $emails[] = $email; //Add email to "used" emails array
}

Before your foreach loop, create a new empty array that will store the already "known" email addresses.
In your foreach loop test if the new array already contains the email, if no, save it and print it, else, ignore and loop again.

$uniqueArray = array();
foreach ($scouts as $participant) {
   if (!in_array($participant['5'], $uniqueArray)) {
            $fname   = ucfirst($participant['2']);
            $lname    = ucfirst($participant['3']);
            $email    = $participant['5'];
            $html .= "\t<li><a href=>$fname $lname $email</a></li>
";
            $uniqueArray[] = $email;
    }
 }