Reputation: 1498
I want to compare two arrays if an email address in array 1 exists in array 2 (here: [email protected]
). In that case it should display that the email already exists.
$Array1 = [
[
'username' => 'uname1',
'name' => 'fullname1',
'email' => '[email protected]'
],
[
'username' => 'uname2',
'name' => 'fullname2',
'email' => '[email protected]'
],
[
'username' => 'uname3',
'name' => 'fullname3',
'email' => '[email protected]'
],
];
$Array2 = [
[
'username' => 'uname1',
'name' => 'fullname1',
'email' => '[email protected]'
],
];
Upvotes: 0
Views: 2332
Reputation: 47874
PHP has a native function which allows the comparison and filtration of one 2d array by one or more 2d arrays. In the callback of array_uintersect()
using a three-way comparison operator between the targeted column values.
This is an elegant and ideal approach because no data preparation is required.
Code: (Demo)
var_export(
array_uintersect(
$Array2,
$Array1,
fn($a, $b) => $a['email'] <=> $b['email']
)
? 'email already exists'
: 'no matches'
);
If the filtered copy of the first-listed array has any surviving rows, then a truth check will indicate that at least one email was matched. If no rows from the second array had matching email values in the rows of the first array, then "no matches" will be shown.
For anyone unfamiliar with intersect
and diff
array functions with a callback, the $a
and $b
parameters DO NOT strictly corelate to the first and second parameter. In fact, not only might the values carried by these variables come from the opposite ordered array, it is possible that both parameters might come from the same array. This is because under the hood, PHP is engaging a sorting process to optimize the filtering process.
Admittedly, for the task of determining if any intersections exist, array_uintersect()
will not stop as soon as it finds a match so there may be more efficient solutions which conditionally return early.
From PHP8.4, array_any()
can make array_any()
calls to permit a function approach with conditional short circuiting for optimal performance. Demo
var_export(
array_any(
$Array2,
fn($row2) => array_any(
$Array1,
fn($row1) => $row1['email'] === $row2['email']
)
)
? 'email already exists'
: 'no matches'
);
Demonstration of short-circuiting with long-hand callbacks and iterated echos: https://3v4l.org/vcsNo/rfc#vgit.master
Upvotes: 0
Reputation: 11583
It's easy.
$result = array_search($Array2[0], $Array1)
var_dump($result);
If you want to check whether something was found, remember to do it like this:
if ($result !== false) { print "Found!"; }
The reason is that array_search can return integer 0, if result was found at index 0 in $Array1 and writing the check as
if ($result == false) { print "Not found"; }
will print "Not found" in this case.
Upvotes: 0
Reputation: 655189
I would build an index of array 2 where the email address is the key:
$index = array();
foreach ($Array2 as $item) {
$index[$item['email']] = true;
}
Then checking for an existing email address costs only O(1) for every item in array 1:
foreach ($Array1 as $item) {
if (isset($index[$item['email']])) {
echo 'email already exists';
}
}
Upvotes: 1
Reputation: 24577
You might want to consider using the email as the key. Something like this:
$a1 = array();
foreach ($Array1 as $v) $a1[$v['email']] = $v;
$a2 = array();
foreach ($Array2 as $v) $a2[$v['email']] = $v;
$intersection = array_values(array_intersect_key($a1, $a2));
This yields an array that contains all the values of the first array that have an email present in the second array. You can then iterate through that array to display error messages.
Upvotes: 1
Reputation: 40193
Pretty standard.
<?php
function userExists() {
global $Array1, $Array2;
for($Array2 as $user) {
for($Array1 as $existingUser) {
if($user['email'] == $existingUser['email']) {
return true;
}
}
}
return false;
}
if(userExists())
echo 'user exists.';
?>
Upvotes: 0