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/… 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. :)

Sign up to request clarification or add additional context in comments.

2 Comments

U would rather say - Great "steallution" lol Upvoted diegoperini in deep concern of justice.
I misread Niklas Ekman's suggestion that day and now I realize it was stealing. Apologies for the inconvenience. I upvoted the question instead.
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

Comments

2
 array_unique is not intended to work on multi dimensional arrays.

You need to loop the array

array_unique

Comments

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 Comment

That would fail if the order of the elements are different. array_unique($airlines, SORT_REGULAR); is the correct answer.

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.