Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
2  
    
One liner: array_map('unserialize', array_unique(array_map('serialize', $person_row_array)); –  Vlad Preda Jul 3 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 3 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 3 at 16:12
    
@UsmanSharifAmjadKhan if you read the link I sent you the guy say you have to add a toString method –  Freelancer Jul 3 at 16:12

1 Answer 1

up vote 1 down vote accepted

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.