2

How can I delete duplicates in a multidimensional array? I tried several answers found on stackoverflow but none of them works for me in an multi array. The closest answer i found was this:

How to delete duplicates in an array? But that only worked on a single-dimensional array.

For example i have this:

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

How can i let it become this:?

$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
    )
)
Community
  • 1
  • 1
Red
  • 6,599
  • 9
  • 43
  • 85
  • Your input and output doesn't make sense. How to you filter our "gray" for example? It's not clear. – scrowler Aug 29 '16 at 04:54
  • I though it might be possible to filter out the duplicate row and the original. So array 1 matches array 3. Delete both rows. – Red Aug 29 '16 at 05:09
  • Read awnser. It does what i asked. So 'not possible at all' kinda wierd awnser – Red Aug 29 '16 at 05:32
  • @RichardMauritz it's possible(i know,i answered also), what i am saying that keys 3,4,3,4 that's not possible, because identical keys will over-write previous one – Alive to die - Anant Aug 29 '16 at 05:34
  • You wrote errors in your code. Check this out: https://eval.in/630613 – Red Aug 29 '16 at 05:44
  • @RichardMauritz check these links for further assessment (your initial array in which indexes over-writes previous values)https://eval.in/630605 and output after changing your initial array indexes:-https://eval.in/630585 – Alive to die - Anant Aug 29 '16 at 08:14
  • Thanks for all the info. I get it now what u mean by all this. – Red Aug 29 '16 at 08:23

3 Answers3

3

Well, this is a very poor answer but it appears you want UNIQUE data, here's some very bad code to solve this.

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

The @ is there to prevent php from throwing an error. or you can write more code to predefine $array2 but this does work.

Forbs
  • 1,256
  • 1
  • 7
  • 9
1

You could get the count of colors first, so that you could distinguish which will be removed and will be kept.

Then, after that, you'll know which color to keep.

To get the count just use array_count_values to get which to keep. Then finally, filter:

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

This uses array_column which may not be available on your environment, you may use this line as an alternative:

$a = array_count_values(array_map(function($e){ return $e['color']; }, $array));
Kevin
  • 41,694
  • 12
  • 53
  • 70
0

Assume your array is this

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

Code

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

Output

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
        )

)
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • the OP wants to take out (don't include) array values that has duplicates, check out the expected out, and the first answer is already correct – Kevin Aug 29 '16 at 05:01