如何删除像这样的数组中的重复项?
我的数组$ 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 )
)
我该怎么办?
答案 0 :(得分: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);
答案 1 :(得分: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);
}
}
答案 2 :(得分:0)
尝试使用array_unique($ test1)
答案 3 :(得分:0)
您可以将array_intersect_key
与array_unique
和array_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);
答案 4 :(得分:0)
你应该编写自己的函数beacuse 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;
}
答案 5 :(得分: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));