Find duplicates in array of objects on the basis of specific keys

為{幸葍}努か 提交于 2019-12-11 03:26:41

问题


my goal is to find duplicates in an array of objects, but only for specific object-variables.

Instead of using two foreach-loops like the following, I am searching for a better (more elegant) way to find the duplicates:

foreach ($data as $date) {
      foreach ($data as $innerDate) {
          if ($date->birthday == $innerDate->birthday &&
              $date->street == $innerDate->street &&
              $date->streetnr == $innerDate->streetnr &&
              $date->zipcode == $innerDate->zipcode &&
              $date->twinid == $innerDate->twinid &&
              $date !== $innerDate) {
              // Duple
        }
    }
}

Thanks!


Now, I'm using following code, based on Tarilo's idea:

usort($data, function($obj_a, $obj_b){
      if ($obj_a->birthday == $obj_b->birthday &&
          $obj_a->street == $obj_b->street &&
          $obj_a->streetnr == $obj_b->streetnr &&
          $obj_a->zipcode == $obj_b->zipcode &&
          $obj_a->twinid == $obj_b->twinid) {
          // Duple
      }
});

Looks much better than two foreach-Loops ;-)


回答1:


You could sort the array first and then loop over the sorted array. This way you only have to compare the current object with the next/previous object. Your current algorithm is O(n^2) efficient but after sorting it would be (sorting + looping) = (O(log n) + O(n)) efficient. Where n is the number of objects in your array.




回答2:


Did you tried in_array() function in php??

For more reference about in_array() use this url

http://php.net/manual/fr/function.in-array.php




回答3:


Since $data is an array, we can use array_* function

Try this, works on my end (PHP 5.2.0).

if ($data != array_unique($data)) {
    echo 'oops, this variable has one or more duplicate item(s)'; die;
}



回答4:


This one gives you an array with similar items grouped. Should be faster for bigger datasets: O(2n) with additional cost for string concat and count on resulting groups. Just takes a little more memory because of the hashmap.

$hashmap = array();
foreach ($data as $date) {
    $hash = $date->zipcode.'-'.$date->street.'-'.$date->streetnr.'-'.$date->birthday.'-'.$date->twinid;
    if (!array_key_exists($hash, $hashmap)) {
        $hashmap[$hash] = array();
    }
    $hashmap[$hash][] = $date;
}

foreach ($hashmap as $entry) {
    if (count($entry) > 1) {
        foreach ($entry as $date) {
            // $date is a duplicate
        }
    }
}


来源:https://stackoverflow.com/questions/13913229/find-duplicates-in-array-of-objects-on-the-basis-of-specific-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!