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?

Itsmeromka
  • 3,621
  • 9
  • 46
  • 79
  • 5
    Did you try using `array_unique(my_array, SORT_REGULAR)` ? – diegoperini May 03 '13 at 11:25
  • 1
    Possible duplicate - http://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 03 '13 at 11:26
  • 1
    possible duplicate of http://stackoverflow.com/questions/6766942/php-unique-array-function-for-nested-array – Linga May 03 '13 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! :) – Itsmeromka May 03 '13 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. – Niklas Ekman Jul 29 '13 at 14:43

4 Answers4

19
array_unique(my_array, SORT_REGULAR)

As requested in comments. :)

diegoperini
  • 1,796
  • 21
  • 37
  • 1
    U would rather say - Great "steallution" lol Upvoted diegoperini in deep concern of justice. – Arthur Kushman Apr 06 '14 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. – diegoperini Apr 25 '14 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
fullybaked
  • 4,117
  • 1
  • 24
  • 37
2
 array_unique is not intended to work on multi dimensional arrays.

You need to loop the array

array_unique

Shijin TR
  • 7,516
  • 10
  • 55
  • 122
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);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • That would fail if the order of the elements are different. `array_unique($airlines, SORT_REGULAR);` is the correct answer. – cleong May 03 '13 at 11:45