5

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

7
  • 3
    possible duplicate stackoverflow.com/questions/2426557/array-unique-for-objects Commented Jul 3, 2014 at 16:07
  • One liner: array_map('unserialize', array_unique(array_map('serialize', $person_row_array)); Commented Jul 3, 2014 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 Commented Jul 3, 2014 at 16:11
  • 1
    @UsmanSharifAmjadKhan it will be working when You implement correct __toString() method for compare. Read accepted answer from Freelancer link carefully Commented Jul 3, 2014 at 16:12
  • @UsmanSharifAmjadKhan if you read the link I sent you the guy say you have to add a toString method Commented Jul 3, 2014 at 16:12

4 Answers 4

19

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);
}
Sign up to request clarification or add additional context in comments.

Comments

8

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

3 Comments

This code was taken from [php.net/manual/en/function.array-unique.php], right? :)
@DonJoe the link you provided does not work anymore, has it moved?
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');

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.