PHP从数组中删除重复的对象 [英] PHP Removing duplicate objects from array

查看:100
本文介绍了PHP从数组中删除重复的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've tried all sorts of PHP logic and PHP's built in functions to remove duplicate values but it is not working no errors shows up but all my JQuery and CSS doesn't work if use array_unique() in_array() to remove the duplicate objects in my array

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

this is how i am creating my person_row_array inside a while loop:

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

对象print_r如下:

object print_r is below:

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()我已经尝试了最近几个小时了,但仍无法正常工作,但是如果您可以通过其他方法告诉我或者添加对象(如果已经不行的话)退出.

I want to remove those duplicate ones please help and please do not advise me to use array_unique() I've been trying this for last few hours not working but if you could tell me any other way or add object if already not exits.

请注意,这是假数据,我只想删除那些具有相似ID的数组.

Please Note this is fake data i only want to remove those array those have similar ids.

最诚挚的问候

推荐答案

尝试以下代码

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);
}

这篇关于PHP从数组中删除重复的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆