PHP取一个对象数组,摆脱重复的一个

时间:2016-09-20 06:55:54

标签: php arrays object multidimensional-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

如您所见,数组中的两个第一个对象是重复的,如何在没有重复对象的情况下获得相同的数组,这意味着:

$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 

请注意,这是一个示例数组。数组中的项数可以多于三个,其中重复对象的数量可以多于两个。

非常感谢

1 个答案:

答案 0 :(得分:0)

使用用于删除重复的数组唯一函数

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