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 have an array like this

Array
(
    [0] => Array
        (
            [id] => BA
            [name] => British Airways
        )

    [1] => Array
        (
            [id] => BA
            [name] => British Airways
        )

    [2] => Array
        (
            [id] => LA
            [name] => Lanchile
        )

    [3] => Array
        (
            [id] => LA
            [name] => Lanchile
        )

    [4] => Array
        (
            [id] => BA
            [name] => British Airways
        )

    [5] => Array
        (
            [id] => BA
            [name] => British Airways
        )

)

and i want to get

Array
(
    [0] => Array
        (
            [id] => BA
            [name] => British Airways
        )

    [1] => Array
        (
            [id] => LA
            [name] => Lanchile
        )
)

but after using array_unique function, all i have is

Array
(
    [0] => Array
        (
            [id] => BA
            [name] => British Airways
        )

)

what am i doing wrong?

share|improve this question
2  
Did you try using array_unique(my_array, SORT_REGULAR) ? –  diegoperini May 3 '13 at 11:25
1  
Possible duplicate - stackoverflow.com/q/307674/608170. Also if this array is the result of a query, you need to recheck your query so as to eliminate the duplicates. –  verisimilitude May 3 '13 at 11:26
1  
possible duplicate of stackoverflow.com/questions/6766942/… –  ling.s May 3 '13 at 11:27
    
Dont know how to mark comment as an answer, but 'diegoperini' you right, array_unique(my_array, SORT_REGULAR) + sort() solved the problem! :) –  Nerfair May 3 '13 at 13:09
    
Wish I saw these comments 1 hour earlier. I don't think you can mark comments as an answer. You should answer the question yourself, give @diegoperini credit and mark the answer as completed. –  Niklas Ekman Jul 29 '13 at 14:43
add comment

4 Answers

up vote 6 down vote accepted
array_unique(my_array, SORT_REGULAR)

As requested in comments. :)

share|improve this answer
    
Very nice solution, worked for me like a charm :) –  vanemaster Oct 29 '13 at 16:01
add comment

As mentioned array_unique doesn't support multi dimensional arrays, but you could iterate over the data and build your own

<?php
$airlines = array(
    array('id' => 'BA', 'name' => 'British Airways'),
    array('id' => 'LA', 'name' => 'Lanchile'),
    array('id' => 'BA', 'name' => 'British Airways'),
    array('id' => 'LA', 'name' => 'Lanchile'),
    array('id' => 'BA', 'name' => 'British Airways'),
    array('id' => 'LA', 'name' => 'Lanchile'),
);
$tmp = array();
foreach ($airlines as $item) {
    if (!in_array($item['id'], $tmp)) {
        $unique[] = $item;
        $tmp[] = $item['id'];
    }
}

var_dump($unique); // $unqiue will have your desired results in it var_dump was just for testing
share|improve this answer
add comment
$airlines = array(
    array('id' => 'BA', 'name' => 'British Airways'),
    array('id' => 'LA', 'name' => 'Lanchile'),
    array('id' => 'BA', 'name' => 'British Airways'),
    array('id' => 'LA', 'name' => 'Lanchile'),
    array('id' => 'BA', 'name' => 'British Airways'),
    array('id' => 'LA', 'name' => 'Lanchile'),
);


$unique = array_map(
    'unserialize',
    array_unique(
        array_map(
            'serialize',
            $airlines
        )
    )
);

var_dump($unique);
share|improve this answer
    
That would fail if the order of the elements are different. array_unique($airlines, SORT_REGULAR); is the correct answer. –  cleong May 3 '13 at 11:45
add comment
 array_unique is not intended to work on multi dimensional arrays.

You need to loop the array

array_unique

share|improve this answer
add comment

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.