删除数组PHP中的部分类似值

时间:2016-06-09 06:10:56

标签: php arrays

我的变量$data如下:

$data = array(
    [90]=> array(
        [0] => array(
            [userID] => 90
            [dateLogin] =>  23:35:13
            [type] => 28
         ),
        [1]=>array(
            [userID] => 90
            [dateLogin] =>  23:35:10
            [type] => 29
        )
      ),
    [91]=> array(
        [0]=>array(
            [userID] => 91
            [dateLogin] =>  23:35:13
            [type] => 25
         ).
        [1]=>array(
            [userID] => 91
            [dateLogin] =>  23:35:10
            [type] => 23
         )
      ), 
   )

我想要的是删除具有相同时间dateLogin的数组,并且只留下1个数组,其中dateLogin秒是最小的。我想要实现的输出是:

$data = array(
    [90]=> array(
        [0]=>array(
            [userID] => 90
            [dateLogin] =>  23:35:10
            [type] => 29
         ),
     ), 
    [91]=> array(
        [0]=>array(
            [userID] => 91
            [dateLogin] =>  23:35:10
            [type] => 23
        )
     ), 
   )

我尝试了这段代码,但没有工作:

  foreach ($data as $key => $value) {
       foreach ($value as $k => $v) {
           $temp = $v['dateLogin'];
              if($temp> $v['dateLogin']){
                 unset($data_list[$key]);
               }
        }
   }

任何帮助,谢谢

1 个答案:

答案 0 :(得分:2)

如果其他人重复,请保留索引0尝试

foreach ($data as $key => $value) {
   $temp[$key] = $value[0];
}
print_r($temp);