PHP Removing duplicate objects from array

五迷三道 提交于 2020-01-11 07:22:43

问题


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

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;

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
    )

)

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.

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

Best regards


回答1:


try the following code

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



回答2:


You can make it unique for any field (key) like id, name or 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;
}


来源:https://stackoverflow.com/questions/24558484/php-removing-duplicate-objects-from-array

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