Home > Backend Development > PHP Tutorial > PHP 去除数组的重复项

PHP 去除数组的重复项

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:05:10
Original
849 people have browsed it

如何去掉数组里 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        ))
Copy after login

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


回复讨论(解决方案)

$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));
Copy after login
Array(    [0] => Array        (            [id] => 2            [title] => 26        )    [1] => Array        (            [id] => 3            [title] => 28        )    [2] => Array        (            [id] => 7            [title] => 25        ))
Copy after login

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template