array_diff() is an array function in PHP that compares two or more arrays and returns values in the first array that are not in other arrays. This function compares values in an array, not keys .
grammar:
array_diff(array $array1, array $array2, array ...$arrays): array
$array1 : The first array (the benchmark array used for comparison).
$array2, ...$arrays : Other arrays, multiple arrays can be compared as parameters.
This function returns an array containing all elements that appear in $array1 but not in $array2 and other arrays.
Suppose we have a database that stores the user's email address. We hope to compare the uploaded mailbox list with the mailboxes in the database through the array_diff() function to quickly find out the mailbox records that have been uploaded but not in the database, that is, exception records.
Let's first establish a database connection, assuming that the database is named user_db , there is a table users , and there is a field email to store the user's mailbox.
<?php
// Database connection information
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_db";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query all mailboxes in the database
$sql = "SELECT email FROM users";
$result = $conn->query($sql);
// Save the mailboxes in the database into an array
$dbEmails = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$dbEmails[] = $row['email'];
}
}
$conn->close();
?>
In this code, we first connect to the database user_db , and then get all the email addresses from the users table and save them into the $dbEmails array.
Next, we simulate an uploaded mailbox list, which is usually uploaded through a form and stored in an array.
<?php
// Assume these are uploaded mailbox data
$uploadedEmails = [
'user1@example.com',
'user2@example.com',
'user3@example.com',
'user4@example.com'
];
?>
Now that we have the mailbox list $dbEmails in the database and the uploaded mailbox list $uploadedEmails , we can use the array_diff() function to find out the mailbox addresses that are in the upload list but not in the database.
<?php
// Find out the mailbox in the upload list but not in the database
$diffEmails = array_diff($uploadedEmails, $dbEmails);
// Output exception record
if (!empty($diffEmails)) {
echo "The following mailbox does not exist in the database:<br>";
foreach ($diffEmails as $email) {
echo $email . "<br>";
}
} else {
echo "All uploaded mailboxes are present in the database。";
}
?>
Combining all the above code, the complete PHP program is as follows:
<?php
// Database connection information
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_db";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query all mailboxes in the database
$sql = "SELECT email FROM users";
$result = $conn->query($sql);
// Save the mailboxes in the database into an array
$dbEmails = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$dbEmails[] = $row['email'];
}
}
$conn->close();
// Assume these are uploaded mailbox data
$uploadedEmails = [
'user1@example.com',
'user2@example.com',
'user3@example.com',
'user4@example.com'
];
// Find out the mailbox in the upload list but not in the database
$diffEmails = array_diff($uploadedEmails, $dbEmails);
// Output exception record
if (!empty($diffEmails)) {
echo "The following mailbox does not exist in the database:<br>";
foreach ($diffEmails as $email) {
echo $email . "<br>";
}
} else {
echo "All uploaded mailboxes are present in the database。";
}
?>