PHP Array unique values
Asked Answered
J

4

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?

Judgemade answered 3/5, 2013 at 11:22 Comment(5)
Did you try using array_unique(my_array, SORT_REGULAR) ?Copeland
Possible duplicate - https://mcmap.net/q/92266/-how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php/608170. Also if this array is the result of a query, you need to recheck your query so as to eliminate the duplicates.Golda
possible duplicate of #6767442Atlanta
Dont know how to mark comment as an answer, but 'diegoperini' you right, array_unique(my_array, SORT_REGULAR) + sort() solved the problem! :)Judgemade
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 @Copeland credit and mark the answer as completed.Andalusite
C
19
array_unique(my_array, SORT_REGULAR)

As requested in comments. :)

Copeland answered 29/7, 2013 at 14:47 Comment(2)
U would rather say - Great "steallution" lol Upvoted diegoperini in deep concern of justice.Tallie
I misread Niklas Ekman's suggestion that day and now I realize it was stealing. Apologies for the inconvenience. I upvoted the question instead.Copeland
I
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
Integument answered 3/5, 2013 at 11:29 Comment(0)
I
2
 array_unique is not intended to work on multi dimensional arrays.

You need to loop the array

array_unique

Isthmian answered 3/5, 2013 at 11:25 Comment(0)
R
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);
Roseboro answered 3/5, 2013 at 11:35 Comment(1)
That would fail if the order of the elements are different. array_unique($airlines, SORT_REGULAR); is the correct answer.Aminopyrine

© 2022 - 2024 — McMap. All rights reserved.