-1

I have duplicated record from in my array as follow

   Array ( [0] => 
              Array ( 
                [driverId] => 112 
                [amount] => 15 
                [firstname] => Katty 
                )
           [1]=>
              Array ( 
                [driverId] => 112 
                [amount] => 15 
                [firstname] => Katty 
                )
           [2]=>
              Array ( 
                [driverId] => 118 
                [amount] => 15 
                [firstname] => Starkj 
                )
)

I want to remove with driverId so that my array will not have duplicated result. I tried with array_unique but it is unsuccessful. How can i achieve this ?

Achilles
  • 411
  • 1
  • 5
  • 27
  • Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Rakib Aug 30 '17 at 04:53

2 Answers2

1

you can use array_unique() if you use the SORT_REGULAR flag like so:

array_unique($array, SORT_REGULAR);

5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.

Test Results

[akshay@localhost tmp]$ cat test.php
<?php


$array = array(
    array('driverId'=>112,'amount'=>15,'firstname'=>'Katty'),
    array('driverId'=>112,'amount'=>15,'firstname'=>'Katty'),
    array('driverId'=>118,'amount'=>15,'firstname'=>'Starkj')
);

print_r( array_unique($array, SORT_REGULAR) );

?>

Output

[akshay@localhost tmp]$ php test.php
Array
(
    [0] => Array
        (
            [driverId] => 112
            [amount] => 15
            [firstname] => Katty
        )

    [2] => Array
        (
            [driverId] => 118
            [amount] => 15
            [firstname] => Starkj
        )

)
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
0

Got it from a post

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

Hope it will help.

Achilles
  • 411
  • 1
  • 5
  • 27