6

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?

5
  • 5
    Did you try using array_unique(my_array, SORT_REGULAR) ? Commented May 3, 2013 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. Commented May 3, 2013 at 11:26
  • 1
    possible duplicate of stackoverflow.com/questions/6766942/…
    – Linga
    Commented May 3, 2013 at 11:27
  • 1
    Dont know how to mark comment as an answer, but 'diegoperini' you right, array_unique(my_array, SORT_REGULAR) + sort() solved the problem! :) Commented May 3, 2013 at 13:09
  • 1
    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. Commented Jul 29, 2013 at 14:43

4 Answers 4

19
array_unique(my_array, SORT_REGULAR)

As requested in comments. :)

2
  • 1
    U would rather say - Great "steallution" lol Upvoted diegoperini in deep concern of justice. Commented Apr 6, 2014 at 17:17
  • I misread Niklas Ekman's suggestion that day and now I realize it was stealing. Apologies for the inconvenience. I upvoted the question instead. Commented Apr 25, 2014 at 13:39
4

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
2
 array_unique is not intended to work on multi dimensional arrays.

You need to loop the array

array_unique

1
$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);
1
  • That would fail if the order of the elements are different. array_unique($airlines, SORT_REGULAR); is the correct answer.
    – cleong
    Commented May 3, 2013 at 11:45

Your Answer

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

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