Het zou efficiënter zijn om een statement te maken met WHERE IN
om de overeenkomende e-mails te krijgen en vervolgens de resulterende arrays te vergelijken. U kunt array_diff()
gebruiken om het verschil van de arrays te krijgen.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($servername, $username, $password, $db);
echo "Connected successfully\n";
$tags = preg_split("/\,/", $_POST['tags']);
$invalidEmails = array();
$count = 0;
// modify your array how you want it
foreach ($tags as $i => $tag) {
$trim_brackets = trim($tag, '[]');
$trim_quotes = trim($trim_brackets, '"');
$tags[$i] = $trim_quotes;
}
// build placeholders for WHERE IN
$in = str_repeat('?,', count($tags) - 1) . '?';
// prepare query
$stmt = $conn->prepare("SELECT mail FROM dej_colleagues WHERE mail IN ($in)");
// bind an array of values. First params is a type list.
$stmt->bind_param(str_repeat('s', count($tags)), ...$tags);
$stmt->execute();
$results = $stmt->get_result();
// fetch the matching mails into an array
$mails = [];
foreach ($results as $row) {
$mails[] = $row['mail'];
}
// get the difference between the two arrays
$invalidEmails = array_diff($tags, $mails);
if (count($tags) === count($mails)) {
echo "good";
}
var_dump($invalidEmails);