Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i need help on filtering my 2 dimensional array such as example below:

array(29) { 
    [0]=>  array(2) { 
        [0]=>  string(16) "Andorra La Vella" 
        [1]=>  string(2) "AD"
    }
    [1]=>  array(2) { 
        [0]=>  string(16) "Andorra La Vella" 
        [1]=>  string(2) "AD" 
    }
    [2]=>  array(2) { 
        [0]=>  string(16) "Andorra La Vella" 
        [1]=>  string(2) "AD" 
    }
    [3]=>  array(2) { 
        [0]=>  string(16) "Andorra La Vella" 
        [1]=>  string(2) "AD" 
    }
    [4]=>  array(2) { 
        [0]=>  string(12) "Les Escaldes" 
        [1]=>  string(2) "AD" 
    }...

how do i filter any redundant value from my array? such as key[0] has the same value as key[1][2][3] and i want to remove this redundant value from my array.

i tried array_filter() but no luck. i tried array_splice() and unset(), no luck in either one.

does php provides any native array function for this?

thanks,
aji

share|improve this question
    
What is returning the array in the first place? –  Gordon Mar 3 '10 at 8:27

3 Answers 3

If you want to remove duplicates you can find some more info on another thread

Enjoy!

share|improve this answer

Can array_unique() do this? Not sure if it works with nested arrays.

EDIT: no, it can't do it.

Note: Note that array_unique() is not intended to work on multi dimensional arrays.

share|improve this answer
2  
No: Note: Note that array_unique() is not intended to work on multi dimensional arrays. The values are casted to string values and then compared. I don't think that this works. –  Felix Kling Mar 3 '10 at 8:25
    
hi ben, i've tried array_unique() too but didn't work. it return only 1 array from 29 arrrays. thanks, ~aji –  aji Mar 3 '10 at 8:28
    $to_filter = array(); // your array
    $filtered = array(); // unique values

    array_walk($to_filter, function($v, $k) use (&$filtered) {
        if(!in_array($v, $filtered)) {
            $filtered[] = $v;
        }
    });

And how clean it looks with PHP 5.3's anonymous functions..,.

share|improve this answer
    
unfortunately i used PHP 4 :) –  aji Mar 4 '10 at 3:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.