0

I have this array:-

$arr
: array = 
  0: object(myObject) = 
    id: string = 188
    CaseNo: string = 1
    strname: string = Apple
    strContact: string = Alice
  1: object(myObject) = 
    id: string = 188
    CaseNo: string = 1
    strname: string = Apple
    strContact: string = Alice
  2: object(myObject) = 
    id: string = 189
    CaseNo: string = 3
    strname: string = Amazon
    strContact: string = Jules

As you can see, the two first objects in the array are repeated, how can get the same array without the repeated object, meaning:

$arr
: array = 
  0: object(myObject) = 
    id: string = 188
    CaseNo: string = 1
    strname: string = Apple
    strContact: string = Alice
  1: object(myObject) = 
    id: string = 189
    CaseNo: string = 3
    strname: string = Amazon
    strContact: string = Jules 

Please notice that this is an example array. The number of items in the array can be more than three and the number of repeated objects inside of it can be more than two.

Thanks a lot

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Miguel Mas
  • 547
  • 2
  • 6
  • 22
  • 3
    Possible duplicate of [array\_unique for objects?](http://stackoverflow.com/questions/2426557/array-unique-for-objects) – Ravi Sep 20 '16 at 06:59
  • 1
    you see that question ? I think you can help. http://stackoverflow.com/questions/2426557/array-unique-for-objects – Python_96 Sep 20 '16 at 06:59
  • 3
    $arr= array_map("unserialize", array_unique(array_map("serialize", $arr))); echo "
    ";print_r($arr);
    – Anant Kumar Singh Sep 20 '16 at 07:00
  • Miguel Mas its your responsibility to check the answer and mark and up-vote the answer which is correct and more descriptive for you. Please do it. It will help others. Stack work in this way only. – Anant Kumar Singh Oct 07 '16 at 10:46
  • people are not responding and didn't feel there responsibility – Anant Kumar Singh Oct 23 '16 at 04:15

1 Answers1

0

Use array unique function which used to remove duplication

 $a = array(
        0 => array (
            "ID" => 188,
            "CaseNo" => 1,
            "strname" => 'Apple',
            "strContact" => 'Alies'

        ),
        1 => array (
             "ID" => 188,
            "CaseNo" => 1,
            "strname" => 'Apple',
            "strContact" => 'Alies'

        ),
        2 => array (
            "ID" => 189,
            "CaseNo" => 1,
            "strname" => 'Amazon',
            "strContact" => 'Jules'

        ),
);
echo "<pre>";
$ab = array_map("unserialize", array_unique(array_map("serialize", $a)));
print_r($ab);
KARTHI SRV
  • 499
  • 4
  • 20