0

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.

4
  • 1
    Have a look at stackoverflow.com/questions/369602/… (unsetting an element in a php array) Commented May 8, 2013 at 23:36
  • 2
    Did you search the PHP docs? Commented May 8, 2013 at 23:36
  • Yes, but that will remove only value, I want to remove complete index. Commented May 8, 2013 at 23:39
  • @fvu In your case, first I must find duplicates and then unset them. I think there is a function that does both at the same time and I have used it but can't remember right now.. Commented May 8, 2013 at 23:44

4 Answers 4

3

Look at array_unique with SORT_REGULAR flag.

array_unique($your_array, SORT_REGULAR);
Sign up to request clarification or add additional context in comments.

4 Comments

@zavg: you should use SORT_REGULAR, otherwise it performs conversion of inner arrays always producing 'Array'.
@ItayMoav-Malimovka I think that NOTE might come from times before sort_flags added. I really don't see a reason why it should not work now if you can do array(1,2,3) === array(1,2,3).
@clime Ok, agree, it works cause I tested it on 2 elements array.
will try it right away - it works..I have misunderstood the function. Thank you @clime
0

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

2 Comments

can you explain why is there another array? to comapre values or?
Which one? $dict is to create a hash of the values. which allows for easy comparison.
0

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.

1 Comment

Array has no more than 25 'main' indexes, and I know there is a simple way (I did it before) but just can't remember.
0

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

5 Comments

1. what does 'gettype' do? 2. will my original array be owerwritten?
Well gettype will... get type of the value variable. You can store the return array of the function in a variable. $myArray=array_unique_recusive($originalArray);
that was obviously :).how to access than that variable in another loop?
I am not sure what you mean. There is already a loop in this function.
got confused a little bit, it was 03:00AM at my place (now is 07:05 :)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.