Search code examples
phpmultidimensional-arrayarray-unique

How to extract unique values from this multidimensional array?


How to extract unique values from this array. I've tried another suggestion...

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

However because of the unix timestamp it wont work.

Im looking to extract only the second array index unique value and its array so should be left with..

// expected final 
array(
2 => array(...),
3 => array(..)
)



$arr = array (
 0 => 
    array (
     2 => 
       array (
        'date' => 1438173658,
        'user' => 'admin',
     ),
    ),
 1 => 
   array (
      2 => 
       array (
        'date' => 1438007944,
        'user' => 'admin',
    ),
   ),
 2 => 
    array (
        3 => 
          array (
       'date' => 1437746969,
       'user' => 'supes',
      ),
 )
)

Thanks.


Solution

  • Might be a simpler way, but here is one:

    $result = array_intersect_key($arr,
                                  array_unique(array_map(function($v) {
                                                             return current($v)['user'];
                                                         },
                                                         $arr)));