简体   繁体   English

PHP采取一系列对象,摆脱重复的对象

[英]PHP taking an array of objects, get rid of the duplicates one

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 非常感谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM