-4

I've an array like this:

array(
    0 => array(1, 2, 3, 4),
    1 => array(1, 3, 2, 4),
    2 => array(1, 2, 4, 3),
    3 => array(1, 2, 5, 6)
)

I have to remove records that are repeated. So I need finally to have this array:

array(
    0 => array(1, 2, 3, 4),
    3 => array(1, 2, 5, 6)
)

The script is in PHP.

Who can help? :)

JakubKubera
  • 396
  • 1
  • 2
  • 14

3 Answers3

2

This should work for you:

Just go through each sub array with array_map() and sort() the arrays. Then simply return them implode()'ed. With the created array you can just use array_unique() and then explode() the values again.

<?php

    $result = array_map(function($v){
        return explode(",", $v);
    }, array_unique(array_map(function($v){
            sort($v);
            return implode(",", $v);
        }, $arr)));

    print_r($result);

?>

output:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [3] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 5
            [3] => 6
        )

)
Rizier123
  • 57,440
  • 16
  • 89
  • 140
0

Another way to do it is

$filter = array();
$result = array_filter($array, function($e) use (&$filter) {
    $sum = array_sum($e);
    $ret = isset($filter[$sum]);
    $filter[$sum] = true;

    return !$ret;
});

Demo

Federkun
  • 33,973
  • 8
  • 70
  • 82
0

This must be the easy to understand

foreach($arr as $key => $value){
    sort($value);
    $arr[$key] = $value;
}
$result = array_unique($arr,SORT_REGULAR);
print_r($result);

Output:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [3] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 5
            [3] => 6
        )

)

DEMO

Narendrasingh Sisodia
  • 20,667
  • 5
  • 43
  • 53