PHP从数组中删除重复的对象

4

我尝试了各种 PHP 逻辑和 PHP 内置函数来删除重复的值,但是如果使用 array_unique() in_array() 来删除数组中的重复对象,就不会出现错误,但我的所有 JQuery 和 CSS 都无法工作。

这是我在 while 循环中创建 person_row_array 的方式:

$person_row = Person::findByID($pi_claimant_row->person_id);
$person_row_array[] = $person_row;

以下是 print_r 对象:

Array
(
[0] => stdClass Object
    (
        [id] => 12
        [flat_no] => 
        [house_no] => *
        [street] => 
        [town] => 
        [postcode] => *
        [county] => 
    )

[1] => stdClass Object
    (
        [id] => 14
        [flat_no] => 2
        [house_no] => 33
        [street] => Street
        [town] => Town
        [postcode] => BB
        [county] => County
    )

[2] => stdClass Object
    (
        [id] => 14
        [flat_no] => 2
        [house_no] => 33
        [street] => Street
        [town] => Town
        [postcode] => BB
        [county] => County
    )

[3] => stdClass Object
    (
        [id] => 14
        [flat_no] => 2
        [house_no] => 33
        [street] => Street
        [town] => Town
        [postcode] => BB
        [county] => County
    )

[4] => stdClass Object
    (
        [id] => 15
        [flat_no] => FN
        [house_no] => HN
        [street] => Street
        [town] => Nelson
        [postcode] => PC
        [county] => Manchester
    )

[5] => stdClass Object
    (
        [id] => 15
        [flat_no] => FN
        [house_no] => HN
        [street] => Street
        [town] => Nelson
        [postcode] => PC
        [county] => Manchester
    )

[6] => stdClass Object
    (
        [id] => 16
        [flat_no] => FN
        [house_no] => house
        [street] => Manchester Road
        [town] => Town
        [postcode] => M1 4MK
        [county] => County
    )

)

我想删除那些重复的数据,请帮忙,但请不要建议我使用array_unique(),我已经尝试了几个小时,但它并没有起作用。如果你能告诉我其他方法或在不存在对象时添加对象,那就太好了。
请注意,这是虚假数据,我只想删除那些具有相似id的数组。
最好的问候。

3
可能是重复问题 https://dev59.com/lXE95IYBdhLWcg3wKqyq - Freelancer
一行代码:array_map('unserialize', array_unique(array_map('serialize', $person_row_array))); - Vlad Preda
@自由职业者 我说过我尝试了array_unique,但它没有起作用。如果你听不懂英语,请告诉我你说的是哪种语言,我会将其翻译成你的语言。否则,让我们继续用英语交流。 - Amjad
1
@UsmanSharifAmjadKhan 当您为比较实现正确的__toString()方法时,它将正常工作。请仔细阅读Freelancer链接中的已接受答案。 - szymanskilukasz
@UsmanSharifAmjadKhan 如果你看了我发给你的链接,那个人说你需要添加一个toString方法。 - Freelancer
显示剩余2条评论
4个回答

18

尝试以下代码

function my_array_unique($array, $keep_key_assoc = false){
    $duplicate_keys = array();
    $tmp = array();       

    foreach ($array as $key => $val){
        // convert objects to arrays, in_array() does not support objects
        if (is_object($val))
            $val = (array)$val;

        if (!in_array($val, $tmp))
            $tmp[] = $val;
        else
            $duplicate_keys[] = $key;
    }

    foreach ($duplicate_keys as $key)
        unset($array[$key]);

    return $keep_key_assoc ? $array : array_values($array);
}

7
您可以将其针对任何字段(键)使其变得唯一,例如id、名称或num。
function unique_multidimensional_array($array, $key) {
    $temp_array = array();
    $i = 0;
    $key_array = array();

    foreach($array as $val) {
        if (!in_array($val[$key], $key_array)) {
            $key_array[$i] = $val[$key];
            $temp_array[$i] = $val;
        }
        $i++;
    }
    return $temp_array;
}

1
这段代码是从[https://www.php.net/manual/en/function.array-unique.php]中提取的,对吧? :) - DonJoe
@DonJoe,您提供的链接已经失效了,它是否已经更改了位置? - Seb
@Seb 这个链接:https://www.php.net/manual/zh/function.array-unique.php - Mërgim Uka

1
这个是什么意思?
$dedup = function($array, $key) {
  $result = [];
  foreach($array as $i) {
    if(!isset($result[$i->{$key}])) {
      $result[$i->{$key}] = $i;
    }
  }
  // sort($result); <-- Add this if you want to clean up the keys.
  return $result;
};

$persons_without_duplicates = $dedup($person_row_array, 'id');

0

另一种解决答案收集的方法 :3

这里使用了array_filter来比较两个数组,由于我们使用了指针,所以可以很好地进行比较。

注意:- "这也意味着即使我们在对象内部有其他对象,或者如果我们有一个包含对象的数组(等等),我们仍然能够对它们进行过滤,因为我们是在比较它们,而不是在寻找其中的某些内容"。


 if (!empty($array)) {
    $array = array_filter(
        $array, /* <- Input for the array_filter to iterate over. */
        function($layout, $index) use(&$array) {
            if ($index === 0) { return; } // <- return; === false;
    
            /* Check if they are the exact same, this works because 
               we are using the use(&$array) (this is a pointer, like in C++/C/etc.) */
            if ($layout === $array[$index-1]) {
                return false;
            }
        }, 
        ARRAY_FILTER_USE_BOTH /* <- So we can use both the value and index. */
    );
}

希望这能有所帮助 :3 祝你度过愉快的一天。 (顺便说一句,我不确定这是否有效,如果你对此有更好的答案/解决方案/优化方法,请随意留下评论)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接