Home > OS >  How to remove duplicates value of same key of multidimensional array in PHP
How to remove duplicates value of same key of multidimensional array in PHP

Time:06-13

I have a multidimensional array and need to remove entries with the same values for a key.

eg.

Array
(
[0] => stdClass Object
    (
        [id] => 1177930857
        [lat] => 24.479280471802
        [lon] => 46.973030090332
        [status] => completed
        [status_desc] => Order Completed
        [date] => 2022-01-05T14:38:53.433Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[1] => stdClass Object
    (
        [id] => 1177930856
        [status] => first_delivery_attempt
        [status_desc] => First delivery attempt
        [date] => 2022-01-05T14:38:53.429Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[2] => stdClass Object
    (
        [id] => 1177930566
        [lat] => 24.479286193848
        [lon] => 46.973037719727
        [status] => picked_up
        [status_desc] => Picked Up
        [date] => 2022-01-05T14:38:09.673Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[3] => stdClass Object
    (
        [id] => 1177930563
        [status] => third_pickup_attempt
        [status_desc] => Third pickup attempt
        [date] => 2022-01-05T14:38:09.663Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[4] => stdClass Object
    (
        [id] => 1177930281
        [lat] => 24.479211807251
        [lon] => 46.973079681396
        [status] => on_his_way
        [status_desc] => On his way
        [date] => 2022-01-05T14:37:23.809Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[5] => stdClass Object
    (
        [id] => 1177929996
        [lat] => 24.479232788086
        [lon] => 46.973007202148
        [status] => accepted
        [status_desc] => Accept
        [date] => 2022-01-05T14:36:50.781Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[6] => stdClass Object
    (
        [id] => 1177906778
        [lat] => 24.479225158691
        [lon] => 46.97301864624
        [status] => picked_up
        [status_desc] => Picked Up
        [date] => 2022-01-05T14:14:07.153Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[7] => stdClass Object
    (
        [id] => 1177906776
        [status] => second_pickup_attempt
        [status_desc] => Second pickup attempt
        [date] => 2022-01-05T14:14:07.143Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[8] => stdClass Object
    (
        [id] => 1177899225
        [lat] => 24.479236602783
        [lon] => 46.973030090332
        [status] => on_his_way
        [status_desc] => On his way
        [date] => 2022-01-05T14:11:15.398Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[9] => stdClass Object
    (
        [id] => 1177897941
        [lat] => 24.479259490967
        [lon] => 46.973037719727
        [status] => accepted
        [status_desc] => Accept
        [date] => 2022-01-05T14:10:18.301Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[10] => stdClass Object
    (
        [id] => 1175904301
        [lat] => 24.479475021362
        [lon] => 46.973045349121
        [status] => picked_up
        [status_desc] => Picked Up
        [date] => 2022-01-03T15:01:23.370Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[11] => stdClass Object
    (
        [id] => 1175904299
        [status] => first_pickup_attempt
        [status_desc] => First pickup attempt
        [date] => 2022-01-03T15:01:23.360Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[12] => stdClass Object
    (
        [id] => 1175903039
        [lat] => 24.479200363159
        [lon] => 46.973037719727
        [status] => on_his_way
        [status_desc] => On his way
        [date] => 2022-01-03T15:00:54.959Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

[13] => stdClass Object
    (
        [id] => 1175446498
        [status] => accepted
        [status_desc] => Accept
        [date] => 2022-01-03T06:30:00.000Z
        [driver] => stdClass Object
            (
                [id] => 1175437372
                [name] => xxxxxx
                [login] => [email protected]
                [email] => [email protected]
                [status] => active
            )

    )

)

In this array, there is a field status, and I need to keep only one entry with the same status which means I need picked_up status only once. And I am expecting the last entry with the specified status in the result array. I need to apply the same logic to all statuses, eg. on_his_way, accepted etc.

CodePudding user response:

If you need only latest entry for each unique status, you can simply use status's value as a key in another associative array and keep overriding it at every iteration in the loop. In the end, you will only be left with unique results with latest entries.

Say, you have this array in a variable called $data, then you could loop like below:

<?php

$set = [];

foreach($data as $o){
  $set[ $o->status ] = $o;
}

$set = array_values($set);// if you don't like status keys

print_r($set);

CodePudding user response:

$input = array_map("unserialize" , array_unique(array_map("serialize", $input)));

array_map - Applies the callback to the elements of the given array

serialized - Generates a storable representation of a value

unserialize - Creates a PHP value from stored representation

  • Related