PHP compare 2 different arrays by a value and find differences

I have 2 very completely different arrays:
1 looks like this ($fromBackend):
array:3 [
114466 => array:1 [
"BrandID" => 136,
"record" => 114466
]
114467 => array:1 [
"BrandID" => 1,
"record" => 114467
]
114468 => array:1 [
"BrandID" => 2,
"record" => 114468
]
]
The other one looks like this ($fromFrontend):
array:2 [
0 => Entity {#920
#id: 117
#brandId: 1
}
1 => Entity {#915
#id: 118
#brandId: 2
}
]
Now as you can see I need to find the "record" of the first array where the brandId's dont match so in this case I need to find "114466" because BrandId => 136 doesn't exist in the second array
Here's what I've got:
$recordsToDelete = [];
foreach ($fromBackend as $fromBackendItem) {
foreach ($fromFrontend as $fromFrontendItem) {
if ($fromBackendItem['BrandID'] !== $brand->getBrandId()) {
$recordsToDelete[] = $fromBackendItem['record'];
}
}
}
dd($recordsToDelete);
This gets me a weird results probably cause I am looping too much:
0 => 114466
1 => 114466
2 => 114467
3 => 114468
Answer
Solution:
You're adding every record to your$recordsToDelete
array because any time thebrandId
values don't match, you add the record and, since thebrandId
values don't match for all records, that means that condition is always true at least once for every record. Instead, compare all thebrandId
values from the front end for each back end record and only add the record to$recordsToDelete
if it doesn't match any otherbrandId
value. For example:
$recordsToDelete = array();
foreach ($fromBackend as $backendItem) {
foreach ($fromFrontEnd as $frontendItem) {
if ($frontendItem->brandId == $backendItem['BrandID']) break;
}
if ($frontendItem->brandId != $backendItem['BrandID']) $recordsToDelete[] = $backendItem['record'];
}
print_r($recordsToDelete);
Output (for your sample data):
Array
(
[0] => 114466
)
If$fromFrontEnd
could be empty the above code will raise an undefined variable error. You can work around that by checking it using the null coalescing operator e.g.
if (($frontendItem->brandId ?? -1) != $backendItem['BrandID']) $recordsToDelete[] = $backendItem['record'];
or by using an explicitfound
flag:
foreach ($fromBackend as $backendItem) {
$found = false;
foreach ($fromFrontEnd as $frontendItem) {
if ($frontendItem->brandId == $backendItem['BrandID']) {
$found = true;
break;
}
}
if (!$found) $recordsToDelete[] = $backendItem['record'];
}
although it would probably be simpler to just not execute the outerforeach
loop unless!empty($fromFrontEnd)
;
Demo of above 2 solutions on 3v4l.org
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xmlhttprequest error flutter
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.