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_key一緒に使用できます。array_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 に答える