I have PHP code below that will check for two associative arrays. My problem is that, this code perform many loops and I don't want it cause it affects the performance of my application. Is there any way or function in PHP that will automatically detect for duplicate? And result with the same as mine.
Please check sample here.
Here is my code:
// first Array
$m1Array = array();
$m1Array[] = array('eId' => '0001', 'numVal' => 1);
$m1Array[] = array('eId' => '0002', 'numVal' => 2);
$m1Array[] = array('eId' => '0003', 'numVal' => 3);
$m1Array[] = array('eId' => '0004', 'numVal' => 4);
$m1Array[] = array('eId' => '0005', 'numVal' => 5);
$m1Array[] = array('eId' => '0006', 'numVal' => 6);
//second Array
$m2Array = array();
$m2Array[] = array('eId' => '0001', 'numVal' => 1);
$m2Array[] = array('eId' => '0004', 'numVal' => 4);
$m2Array[] = array('eId' => '0005', 'numVal' => 5);
$m2Array[] = array('eId' => '0006', 'numVal' => 6);
$m2Array[] = array('eId' => '0007', 'numVal' => 7);
//final result array
$finalResult = array();
//[second array] will be my master or the bases
//loop thru the [second array(m2Array)]
foreach($m2Array as $m2Arr){
$numValSum = 0;
$dupFound = false;
//get current eId value in [second array]
$eId2 = $m2Arr['eId'];
//loop thru the [first array(m1Array)] to check if eId2 has duplicate
$arrIndex = 0;
foreach($m1Array as $m1Arr){
//get current eId value in [first array]
$eId1 = $m1Arr['eId'];
//check if the value of eId2 is equal to eId 1
if($eId1 == $eId2){
//if equal then
//add their respective numVal value and put it to [final result array]
$numValSum = $m2Arr['numVal'] + $m1Arr['numVal'];
$finalResult[] = array('eId' => $eId2, 'numValSum' => $numValSum);
unset($m1Array[$arrIndex]); //remove the duplicate eId in [first array]
sort($m1Array); //sort the index of [first array]
$dupFound = true;
}
$arrIndex += 1;
}
//if eId2 has no duplicate then just add the numVal to [final result array]
if($dupFound == false){
$finalResult[] = array('eId' => $eId2, 'numValSum' => $m2Arr['numVal']);
}
}
//now check if [second array] still have an element
//if still have then add all to [final result array]
if(count($m1Array)){
foreach($m1Array as $m1Arr){
$finalResult[] = array('eId' => $m1Arr['eId'], 'numValSum' => $m1Arr['numVal']);
}
}
//display my final result array
foreach($finalResult as $fRes){
echo $fRes['eId'].' -> '.$fRes['numValSum'].PHP_EOL;
}
Additional Info.:
Apologies, but I think some of you missed the point of my code. I don't want to remove duplicates in my arrays. The code will check for duplicate bet. two arrays and if the eId
has a duplicate in another array then add their corresponding numVal
before putting it to my output
or finalResult array
. But if don't have duplicate then put it directly to my finalResult array
.
I attached a table diagram image of how it works.
You can reduce the 59
lines of code to 3
by using array_udiff()
to get the difference between two multidimensional arrays. It accepts a function for data comparision:
var_dump(array_udiff($m1Array, $m2Array, function($a, $b) {
return $a["eId"] - $b["eId"];
}));
There is no such function in php, so you can break it down to pieces:
$finalResult = $a1 = array_column($m1Array, "numVal", "eId");
$a2 = array_column($m2Array, "numVal", "eId");
array_walk($finalResult, function(&$item, $key) use ($a2) {
if(isset($a2[$key])) $item += $a2[$key];
});
$finalResult = array_merge($finalResult, array_diff_key($a2, $finalResult));
// Array ( [0001] => 2 [0002] => 2 [0003] => 3 [0004] => 8 [0005] => 10 [0006] => 12 [0007] => 7 )