mihajlo
mihajlo

Reputation: 108

PHP - remove index with duplicated values

I have an 2D array, it is simple as this:

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

[1] => Array
    (
        [0] => 6
        [1] => 6
        [2] => 11
    )

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

Of course, they are inside of another array. What I want is to remove index[2] because it has same values as index[0]. I searched here and on google but couldn't find how to solve issue exactly like this one. Thank you in advance.

Upvotes: 0

Views: 1582

Answers (4)

RST
RST

Reputation: 3925

try this

function array_unique_recusive($arr){
foreach($arr as $key=>$value)
if(gettype($value)=='array')
    $arr[$key]=array_unique_recusive($value);
return array_unique($arr,SORT_REGULAR);
}

as mentioned here http://php.net/manual/en/function.array-unique.php

Upvotes: 0

Xeos
Xeos

Reputation: 6497

I can suggest having a hash sum calculated for every sub-array in case you have many sub-arrays with many values. Then store that hash into a new array or as an element of the sub-array. Then itterate through comparing hashes and unset($foundArray); for matches.

Upvotes: 0

clime
clime

Reputation: 8885

Look at array_unique with SORT_REGULAR flag.

array_unique($your_array, SORT_REGULAR);

Upvotes: 3

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53603

don't crare too much on performance.

$dict = array();
foreach($your_data as $one_index){
  $dict[join('',$one_index)]=$one_index;
}

$res=array();
foreach($dict as $one_index){
   $res[] = $one_index;
}

var_dump($res);

Upvotes: 0

Related Questions