Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php remove duplicates from multidimensional array by value [duplicate]

I would like to remove duplicates by value as you can see from the list_title. I know there are several questions and answers to this but their solution doesn't work for me.

Here is what I've tried:

$uniqueArray = array_map("unserialize", array_unique(array_map("serialize", $notify)));

Result:

Array
(
[0] => Array
    (
        [list_id] => 86
        [list_reference] => 130948
        [list_title] => Offer:  apartment 2+kk 
        [list_city] => Prague
        [list_date] => 2017-03-03 11:20:35
        [list_status] => 0
        [list_creator] => Company A
        [list_price] => 30000
        [list_furniture] => ["1","0","0"]
        [list_accommodation] => flat
    )

[1] => Array
    (
        [list_id] => 87
        [list_reference] => 130947
        [list_title] => Offer:  apartment 2+kk 
        [list_date] => 2017-03-03 11:20:35
        [list_status] => 0
        [list_creator] => Company B
        [list_price] => 30000
        [list_furniture] => ["1","0","0"]
        [list_accommodation] => flat
    )

[2] => Array ...

Expected result should be one of those because of the title:

Array
(
[0] => Array
(
    [list_id] => 86
    [list_reference] => 130948
    [list_title] => Offer:  apartment 2+kk 
    [list_city] => Prague
    [list_date] => 2017-03-03 11:20:35
    [list_status] => 0
    [list_creator] => Company A
    [list_price] => 30000
    [list_furniture] => ["1","0","0"]
    [list_accommodation] => flat
)
like image 446
Shina Avatar asked Mar 03 '17 11:03

Shina


People also ask

How do you get unique values in a multidimensional array?

A user-defined function can help in getting unique values from multidimensional arrays without considering the keys. You can use the PHP array unique function along with the other array functions to get unique values from a multidimensional array while preserving the keys.

How do you remove duplicates from an array in PHP?

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.

How do you remove duplicate values from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.


2 Answers

So basically you want to remove duplicates by 'list_title' column. Let's assume we keep the first occurrence of this title. Then you can use a couple of standard functions to achieve this:

// Reverse array, so the first occurrence kept.
$data = array_reverse($data);

$result = array_reverse( // Reverse array to the initial order.
    array_values( // Get rid of string keys (make array indexed again).
        array_combine( // Create array taking keys from column and values from the base array.
            array_column($data, 'list_title'), 
            $data
        )
    )
);

Here is working demo.

UPDATE:

Based on @mickmackusa comment, the code can be simplified to:

$result = array_reverse(array_values(array_column(
    array_reverse($data),
    null,
    'list_title'
)));

This is described in the docs regarding column_key parameter specs:

It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).

like image 72
sevavietl Avatar answered Sep 30 '22 02:09

sevavietl


Thanks everyone, sometimes over-thinking can affect common sense.

I simply solved it from MYSQL query with something like:

 GROUP BY list_title ORDER BY list_date
like image 37
Shina Avatar answered Sep 30 '22 00:09

Shina