4

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

Amjad
  • 1,950
  • 5
  • 26
  • 41
  • 3
    possible duplicate http://stackoverflow.com/questions/2426557/array-unique-for-objects – Freelancer Jul 03 '14 at 16:07
  • One liner: `array_map('unserialize', array_unique(array_map('serialize', $person_row_array));` – Vlad Preda Jul 03 '14 at 16:09
  • @Freelancer i said i tried array_unique it is not working if you cant understand English tell me which language you speak i'll translate that into your language.. otherwise – Amjad Jul 03 '14 at 16:11
  • 1
    @UsmanSharifAmjadKhan it will be working when You implement correct __toString() method for compare. Read accepted answer from Freelancer link carefully – szymanskilukasz Jul 03 '14 at 16:12
  • @UsmanSharifAmjadKhan if you read the link I sent you the guy say you have to add a toString method – Freelancer Jul 03 '14 at 16:12
  • So what is the root cause here? Why do you have duplicate entries in the array? It would seem that where you generate the value that get's populate into `$pi_claimant_row->person_id` is where you would need to solve this problem. In other words, don't put your effort into de-duping the array, put it into making sure you don't get dupes to being with. – Mike Brant Jul 29 '14 at 16:34
  • Does this answer your question? [array\_unique for objects?](https://stackoverflow.com/questions/2426557/array-unique-for-objects) – Taz Jun 03 '22 at 09:22

4 Answers4

18

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);
}
Sharif
  • 708
  • 1
  • 7
  • 17
7

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;
}
Mërgim Uka
  • 331
  • 5
  • 11
1

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');
jiv-e
  • 483
  • 6
  • 8
0

Another solution to the collection of answers :3

This uses the array_filter to compare the two arrays, and since we are using the pointer, we can compare them just fine.

Note: - "This also means that even if we have objects inside the objects, or if we had an array with objects inside (and so on), we would still be able to filter them, since were comparing them, and not looking for something inside them".


 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. */
    );
}

Hope this helps :3 And have a wonderful day. (Btw idk if this actually efficient, so if you have a better answer/solution/optimization to this, please feel free to leave a comment).