1

Edit

In the link above it does not say anything about to serach for two key values. And I can not find any comments above which Alg that is fastest.

// Raffe

I got two multiarrays that I would like to check for duplicates regarding two columns. I have two alg that works but I do not know what is the fastest way to check or if there are any fater way to do it. The arrays are called: SQL and Sharepoint. Are there ant better and faster way to do this?

Alg1

foreach($SQL as $data1) {    
  foreach($Sharepoint as $data2) {     
    if( ($data1[0] === $data2[0]) && ($data1[1] === $data2[1])) $duplicate = true;
 }
}

Alg2

foreach($SQL as $data1) {    

  $keys = array_keys(array_column($Sharepoint, 0), $data1[0]);
  $tmp1 = array_map(function($k) use ($Sharepoint){return $Sharepoint[$k];}, $keys);
  $keys = array_keys(array_column($tmp1 , 1), $data1[1]); 
  $tmp2 = array_map(function($k) use ($tmp1 ){return $tmp1 [$k];}, $keys);
  if (count($tmp2) > 0) $duplicate = true;
}

Raffe
  • 19
  • 4
  • You can check it by yourself on any online PHP running platform like : https://3v4l.org/ or you can add timer in your code and check as well on local machine – Alive to die - Anant Sep 15 '22 at 06:53
  • given a variable called $SQL, definitely the fastest way would be finding duplicates in the **SQL query** – Your Common Sense Sep 15 '22 at 07:00
  • The data is not in SQL so I can not use that – Raffe Sep 15 '22 at 07:50
  • I did a comparaison here: https://onlinephp.io/c/53b71 Alg2 is better than Alg1 and I did an Alg4 that is way faster, but a bit ugly. You can reuse the code with your real $Sharepoint and $SQL arrays to check the performances with the real data. – Bl457Xor Sep 15 '22 at 13:37
  • Hello Bl457Xor, I looked at the code for alg 4. I do not understand it. How can I see the mismatch? – Raffe Sep 16 '22 at 06:32
  • @Raffe basically instead for working with the "complex" data structure of $SQl and $Sharepoint arrays I copy the useful data for this case in 2 simple arrays. This should be advantageous for big arrays (as it is it works only for data of string type). If `$SQL = [0 => [0 => 'car', 1 => 'red'], 1 => [0 => 'bus', 1 => 'grey'] ]` I create an array `$compactSQL = [0 => 'car¬red', 1 => 'bus¬grey']` I do the same for the $Sharepoint arrray and then the function array_intersect will check if in the $compactSharepoint array there is 'car¬red' or 'bus¬grey' and return all the duplicates in an array. – Bl457Xor Sep 16 '22 at 07:15
  • The "complex" arrays duplicate check is now just a simple arrays duplicate check. The separator (in my example `¬`) should be carefully choosen because if it is present in the data it could lead to false positives (it can be longer than 1 char). array_intersect compare the string representation of the values so if the data contains dates or other types of data, this data should be converted into string when creating the simple arrays – Bl457Xor Sep 16 '22 at 07:27
  • @Bl457Xor I do not see that I get the duplicate variable for each loop (foreach($SQL as $data1) ? I have to do different things dependent of this variable. – Raffe Sep 16 '22 at 08:05
  • @Raffe If I understand it right, you need to do an operation on a $SQL element if a duplicate is found for that element. I modified the Alg4 code so you can find all the $SQL elements that have a duplicate: [code](https://onlinephp.io/c/d85d5) – Bl457Xor Sep 16 '22 at 08:44
  • @Bl457X Thanks!! How do I get the opposite? The elements that does not match? – Raffe Sep 16 '22 at 09:00
  • @Raffe for the Alg4 you may use array_diff($compactSQL,$compactSharepoint) instead of or in addition of array_intersect() to obtain an array with only the keys of $SQL for the elements that don't match. – Bl457Xor Sep 16 '22 at 10:10

0 Answers0