首頁 >後端開發 >php教程 >PHP 去除数组的重复项

PHP 去除数组的重复项

WBOY
WBOY原創
2016-06-23 13:05:10824瀏覽

如何去掉数组里 id相同的项

Array(    [0] => Array        (             [id] => 2            [title] => 26        )    [1] => Array        (              [id] =>3            [title] => 28        )    [2] => Array        (             [id] => 2            [title] => 26        )    [3] => Array        (            [id] => 7            [title] => 25        ))

最终结果
Array(    [0] => Array        (             [id] => 2            [title] => 26        )    [1] => Array        (              [id] =>3            [title] => 28        )    [3] => Array        (            [id] => 7            [title] => 25        ))


回复讨论(解决方案)

$a = Array(  Array( 'id' => 2, 'title' => 26 ),  Array( 'id' => 3, 'title' => 28 ),  Array( 'id' => 2, 'title' => 26 ),  Array( 'id' => 7, 'title' => 25 ),);$res = array();foreach($a as $v) {  if(! isset($res[$v['id']])) $res[$v['id']] = $v;}print_r(array_values($res));
Array(    [0] => Array        (            [id] => 2            [title] => 26        )    [1] => Array        (            [id] => 3            [title] => 28        )    [2] => Array        (            [id] => 7            [title] => 25        ))

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn