如何从多维数组中删除重复项和原始项?

时间:2016-08-29 04:44:28

标签: php arrays

如何删除多维数组中的重复项? 我尝试了在stackoverflow上找到的几个答案,但它们在多数组中都不适用于我。我找到的最接近的答案是:

How to delete duplicates in an array? 但这只适用于一维阵列。

例如我有这个:

$array = array(
    [0] = array(
        [color] => red,
        [type] => color,
        [name] => color1
    )
    [1] = array(
        [color] => gray,
        [type] => color,
        [name] => color2
    )
    [2] = array(
        [color] => blue,
        [type] => color,
        [name] => color3
    )
    [3] = array(
        [color] => green,
        [type] => color,
        [name] => color4
    )
    [4] = array(
        [color] => black,
        [type] => color,
        [name] => color5
    )
    [3] = array(
        [color] => gray,
        [type] => color,
        [name] => color2
    )
    [4] = array(
        [color] => blue,
        [type] => color,
        [name] => color3
    )
)

我怎样才能让它成为现实:?

$array = array(
    [0] = array(
        [color] => red,
        [type] => color,
        [name] => color1
    )
    [1] = array(
        [color] => gray,
        [type] => color,
        [name] => color2
    )
    [2] = array(
        [color] => blue,
        [type] => color,
        [name] => color3
    )
    [3] = array(
        [color] => green,
        [type] => color,
        [name] => color4
    )
    [4] = array(
        [color] => black,
        [type] => color,
        [name] => color5
    )
)

3 个答案:

答案 0 :(得分:3)

嗯,这是一个非常糟糕的答案,但看起来你想要UNIQUE数据,这里有一些非常糟糕的代码来解决这个问题。

$array2 = array();
foreach ($array as $val) 
{
  @$array2[$val['color']]++;
}
foreach ($array as $key=>$val) 
{
  if ($array2[$val['color']] >1)
   unset($array[$key]);
}

@是为了防止php抛出错误。或者您可以编写更多代码来预定义$array2,但这确实有用。

答案 1 :(得分:1)

您可以先获取颜色计数,这样就可以区分哪些颜色会被删除并保留。

然后,在那之后,你就会知道要保留哪种颜色。

要获得计数,只需使用array_count_values即可获得。最后,过滤:

$a = array_count_values(array_column($array, 'color'));
$new_array = array_filter($array, function($e) use($a) {
    return $a[$e['color']] === 1; // filter count equal to 1
});

这使用了您的环境可能无法使用的array_column,您可以使用此行替代:

$a = array_count_values(array_map(function($e){ return $e['color']; }, $array));

答案 2 :(得分:0)

假设您的数组是

$array = array(
    '0' => array(
        'color' => 'red',
        'type' => 'color',
        'name' => 'color1'
    ),
    '1' => array(
        'color' => 'gray',
        'type' => 'color',
        'name' => 'color2'
    ),
    '2' => array(
        'color' => 'blue',
        'type' => 'color',
        'name' => 'color3'
    ),
    '3' => array(
        'color' => 'green',
        'type' => 'color',
        'name' => 'color4'
    ),
    '4' => array(
        'color' => 'black',
        'type' => 'color',
        'name' => 'color5'
    ),
    '5' => array(
        'color' => 'gray',
        'type' => 'color',
        'name' => 'color2'
    ),
    '6' => array(
        'color' => 'blue',
        'type' => 'color',
        'name' => 'color3'
    )
);

<强>代码

$input = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo ' <pre>';
print_r($input);

<强>输出

Array
(
    [0] => Array
        (
            [color] => red
            [type] => color
            [name] => color1
        )

    [1] => Array
        (
            [color] => gray
            [type] => color
            [name] => color2
        )

    [2] => Array
        (
            [color] => blue
            [type] => color
            [name] => color3
        )

    [3] => Array
        (
            [color] => green
            [type] => color
            [name] => color4
        )

    [4] => Array
        (
            [color] => black
            [type] => color
            [name] => color5
        )

)