Remove duplicate object in array php

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

asked Jul 3, 2014 at 16:01

AmjadAmjad

1,8905 gold badges26 silver badges39 bronze badges

7

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

answered Jul 29, 2014 at 16:13

Remove duplicate object in array php

SharifSharif

6781 gold badge7 silver badges17 bronze badges

0

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

answered May 30, 2019 at 12:54

Remove duplicate object in array php

Mërgim UkaMërgim Uka

3215 silver badges11 bronze badges

3

What about this?

$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');

answered Apr 11, 2021 at 21:42

jiv-ejiv-e

4656 silver badges8 bronze badges

How remove duplicates from array of objects in PHP?

Removing duplicate values from an array in PHP To remove duplicate values from an array in PHP, use the array_unique() function. If two or more array values are the same, the first appearance will be kept, and the other will be removed. The returned array will hold the first array item's key type.

How do you remove duplicates from array of objects?

To remove the duplicates from an array of objects: Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.

What is use of array_unique in PHP?

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Note: The returned array will keep the first array item's key type.

How can I remove duplicate values from a multi dimensional array in PHP?

array_unique in PHP The array_unique() function are used to remove duplicate data from given array. There are two parameters that will be passed in the array_unique() , first specifying an array that is required and the second is optional specifying the sorting type.