0

我怎样才能删除这样的数组中的重复项?

我的数组 $test1 给了我这个:

Array ( 
[0] => Array ( [id] => 47523 [date] => 12-02-13 14:36:32 ) 
[1] => Array ( [id] => 47523 [date] => 12-02-13 13:56:48 ) 
[2] => Array ( [id] => 38639 [date] => 12-02-13 13:38:51 ) 
[3] => Array ( [id] => 38639 [date] => 12-02-13 13:07:43 )

)

我的数组 $test2 给了我这个:

Array ( 
[0] => Array ( [id] => 47523 [date] => 12-02-13 14:36:32 ) 
[1] => Array ( [id] => 47523 [date] => 12-02-13 13:56:48 ) 
[2] => Array ( [id] => 38639 [date] => 12-02-13 13:38:51 ) 
[3] => Array ( [id] => 38639 [date] => 12-02-13 13:07:43 )
[4] => Array ( [id] => 53241 [date] => 12-02-13 11:02:48 )
)

但我希望输出是最新日期的那种方式

Array ( 
[0] => Array ( [id] => 53241 [date] => 03-02-13 11:02:48 )
)

我能做些什么?

4

6 回答 6

4
$test1 = array ( 
    array ( "id" => 47523, "date" => "12-02-13 14:36:32" ), 
    array ( "id" => 47523, "date" => "12-02-13 13:56:48" ), 
    array ( "id" => 38639, "date" => "12-02-13 13:38:51" ), 
    array ( "id" => 38639, "date" => "12-02-13 13:07:43" )
);
$test2 = array ( 
    array ( "id" => 47523, "date" => "12-02-13 14:36:32" ), 
    array ( "id" => 47523, "date" => "12-02-13 13:56:48" ), 
    array ( "id" => 38639, "date" => "12-02-13 13:38:51" ), 
    array ( "id" => 38639, "date" => "12-02-13 13:07:43" ),
    array ( "id" => 53241, "date" => "12-02-13 11:02:48" )
);
foreach($test2 as $array) {
    if (!in_array($array, $test1)) {
        $new[] = $array;
    }
}
print_r($new);
于 2013-02-12T14:52:39.033 回答
3

首先合并 2 个数组(在这种情况下从 $test2 到 $tes1 ):

foreach($test2 as $id=>$arr){
  $test1[] = $arr;
}

然后按日期对 $test1 进行排序(将最早的日期放在最后,最新的日期放在最前面):

foreach ($test1 as $key => $row) {
    $orderByDate[$key]  = strtotime($row['date']);
}

array_multisort($orderByDate, SORT_DESC, $dataArray);

然后删除重复项(这将保留最新的日期时间并删除较早的日期时间)

$unique = array()
    foreach($test1 as $id => $arr){
      if( in_array($arr->id, $unique ) {
        unset($test1[$id]);
      }
      else {
        array_push($unique, $arr->id);
      }
    }
于 2013-02-12T14:53:12.700 回答
0

尝试使用 array_unique($test1)

于 2013-02-12T14:48:30.843 回答
0

您可以将array_intersect_keywitharray_uniquearray_map用于单行作业:

PHP >5.3.0

$array = array ( 
    0 => array ( "id" => 47523, "date" => "12-02-13 14:36:32" ), 
    1 => array ( "id" => 47523, "date" => "12-02-13 13:56:48" ), 
    2 => array ( "id" => 38639, "date" => "12-02-13 13:38:51" ), 
    3 => array ( "id" => 38639, "date" => "12-02-13 13:07:43" )
);

$array = array_intersect_key($array, array_unique(array_map(function($n){ return $n['id']; }, $array)));

print_r($array);

PHP <5.3.0

$array = array ( 
    0 => array ( "id" => 47523, "date" => "12-02-13 14:36:32" ), 
    1 => array ( "id" => 47523, "date" => "12-02-13 13:56:48" ), 
    2 => array ( "id" => 38639, "date" => "12-02-13 13:38:51" ), 
    3 => array ( "id" => 38639, "date" => "12-02-13 13:07:43" )
);

function mapArray($n){
     return $n['id'];
}

$array = array_intersect_key($array, array_unique(array_map("mapArray", $array)));

print_r($array);
于 2013-02-12T14:57:24.310 回答
0

您应该编写自己的函数,因为 array_unique 不适用于多维数组。

function array_unique_md($array)
{
    $temp = array();
    foreach($array as $key => $value)
    {
        if(!isset($temp[$value['id']])) $temp[$value['id']] = $value['date'];
    }

    $your_structure = array();
    foreach($temp as $key=> $value)
    {
        $your_structure[] = array('id'=>$key,'date'=>$value);
    }

    return $your_structure;
}
于 2013-02-12T14:57:53.917 回答
0

您可以使用array_reduce

$data = array_reduce($data, function($a,$b){
    isset($a[$b['id']]) or $a[$b['id']] = $b;
    return $a;
});

var_dump(array_values($data));

观看现场演示

于 2013-02-12T14:58:08.973 回答