PHP数组比较问题

WBOY
Release: 2016-06-20 12:58:56
Original
856 people have browsed it

$ar1=array(array('id'=>1,"name"=>"张1","sign"=>""),array('id'=>2,"name"=>"张2","sign"=>""),array('id'=>5,"name"=>"张5","sign"=>""));        $ar2=array(array('id'=>1,"name"=>"张1"),array('id'=>2,"name"=>"张2"));
Copy after login


如果ar2的id与ar1的id相同,那么ar1的sign则复制给1,否则不赋值,怎么解决?


回复讨论(解决方案)

$ar1=array(array('id'=>1,"name"=>"张1","sign"=>""),array('id'=>2,"name"=>"张2","sign"=>""),array('id'=>5,"name"=>"张5","sign"=>""));$ar2=array(array('id'=>1,"name"=>"张1"),array('id'=>2,"name"=>"张2"));//先将 $ar2 规格化一下foreach($ar2 as $v) $r[$v['id']] = $v;$ar2 = $r;//接下来就简单了foreach($ar1 as &$v) {  if(isset($ar2[$v['id']])) $v['sign'] = 1;}print_r($ar1);
Copy after login
Array(    [0] => Array        (            [id] => 1            [name] => 张1            [sign] => 1        )    [1] => Array        (            [id] => 2            [name] => 张2            [sign] => 1        )    [2] => Array        (            [id] => 5            [name] => 张5            [sign] =>         ))
Copy after login

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