Seria mais eficiente construir uma instrução com
WHERE IN
para obter os e-mails correspondentes e, em seguida, comparar os arrays resultantes. Você pode usar array_diff()
para obter a diferença das matrizes. <?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);