删除数组中具有相同值的项目

时间:2014-10-17 09:06:42

标签: php arrays

请参阅我的以下数组,我需要能够删除具有相同值的项(例如,Primary,Secondary等)。有没有办法通过php对现有数组执行此操作,或者更容易执行此操作在将项目插入数组之前?

我见过array_unique函数,但不确定我是否可以使用它,因为它是一个多维数组?

Array
(
    [0] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 19
            [value] => Primary
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 0
            [usageCount] => 
        )

    [1] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 20
            [value] => Secondary
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 1
            [usageCount] => 
        )

    [2] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 20
            [value] => Secondary
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 1
            [usageCount] => 
        )

    [3] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 21
            [value] => Groups & Families
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 2
            [usageCount] => 
        )

    [4] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 19
            [value] => Primary
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 0
            [usageCount] => 
        )

    [5] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 20
            [value] => Secondary
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 1
            [usageCount] => 
        )

    [6] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 21
            [value] => Groups & Families
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 2
            [usageCount] => 
        )

    [7] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 22
            [value] => Adults
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 3
            [usageCount] => 
        )

    [8] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 19
            [value] => Primary
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 0
            [usageCount] => 
        )

    [9] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 20
            [value] => Secondary
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 1
            [usageCount] => 
        )

    [10] => SelectAttributeTypeOption Object
        (
            [error] => 
            [ID] => 21
            [value] => Groups & Families
            [th] => TextHelper Object
                (
                )

            [displayOrder] => 2
            [usageCount] => 
        )
)

3 个答案:

答案 0 :(得分:0)

查看array_filter功能:

array_filter( $array, function( $elem ) use ( $value ) {   
  return $elem['value'] !== $value; 
});

希望它有所帮助。

答案 1 :(得分:0)

试试这个:

$new = array_reduce(
    $old,
    function ($result, $item) {
        if (!in_array($item, $result)) {
            $result[] = $item;
        }
        return $result;
    }
);

但是,如果从DB中获取这些对象,最好先使用GROUP BY id或任何唯一的记录标识符过滤结果集,如Finciuc Sergiu建议的那样。

答案 2 :(得分:0)

我希望这会有所帮助

foreach($array as $row) {
    if(!isset($output[$row['value']])) {
        $output[$row['value']] = $row;
    }
}

//reset array keys if you want //$output = array_values($output)