Home > Backend Development > PHP Tutorial > PHP 数组去掉相同项

PHP 数组去掉相同项

WBOY
Release: 2016-06-23 13:05:06
Original
1289 people have browsed it

$a = array(    array("id"=>7, "title"=>"a"),    array("id"=>5, "title"=>"z"),);$b = array(    array("id"=>5, "title"=>"a"),    array("id"=>1, "title"=>"z"),    array("id"=>2, "title"=>"z"),);//去掉 $b中 id 在 $a 中存在的项//结果$b = array(    array("id"=>1, "title"=>"z"),    array("id"=>2, "title"=>"z"),);
Copy after login


回复讨论(解决方案)

$a = array(    array("id"=>7, "title"=>"a"),    array("id"=>5, "title"=>"z"),); $b = array(    array("id"=>5, "title"=>"a"),    array("id"=>1, "title"=>"z"),    array("id"=>2, "title"=>"z"),); foreach($b as $k=>$v)  foreach($a as $t) if($v['id'] == $t['id']) unset($b[$k]);print_r($b);
Copy after login
Copy after login
Array(    [1] => Array        (            [id] => 1            [title] => z        )    [2] => Array        (            [id] => 2            [title] => z        ))
Copy after login
Copy after login

$a = array(    array("id"=>7, "title"=>"a"),    array("id"=>5, "title"=>"z"),); $b = array(    array("id"=>5, "title"=>"a"),    array("id"=>1, "title"=>"z"),    array("id"=>2, "title"=>"z"),); foreach($b as $k=>$v)  foreach($a as $t) if($v['id'] == $t['id']) unset($b[$k]);print_r($b);
Copy after login
Copy after login
Array(    [1] => Array        (            [id] => 1            [title] => z        )    [2] => Array        (            [id] => 2            [title] => z        ))
Copy after login
Copy after login



在循环的过程中移除某个项,接下来的循环不受影响?

foreach 不受影响
for 受影响

Related labels:
source:php.cn
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