"张1","sign"=>""),array('id'=>2,"name"=>"张2","sign"=>""),array('id'=>5,"name"=>"张5","sign"=>"")); &"/> "张1","sign"=>""),array('id'=>2,"name"=>"张2","sign"=>""),array('id'=>5,"name"=>"张5","sign"=>"")); &">
PHP数组比较问题
<br />$ar1=array(array('id'=>1,"name"=>"张1","sign"=>""),array('id'=>2,"name"=>"张2","sign"=>""),array('id'=>5,"name"=>"张5","sign"=>""));<br /> $ar2=array(array('id'=>1,"name"=>"张1"),array('id'=>2,"name"=>"张2"));<br />
$ar1=array(array('id'=>1,"name"=>"张1","sign"=>""),array('id'=>2,"name"=>"张2","sign"=>""),array('id'=>5,"name"=>"张5","sign"=>""));<br />$ar2=array(array('id'=>1,"name"=>"张1"),array('id'=>2,"name"=>"张2"));<br /><br />//先将 $ar2 规格化一下<br />foreach($ar2 as $v) $r[$v['id']] = $v;<br />$ar2 = $r;<br /><br />//接下来就简单了<br />foreach($ar1 as &$v) {<br /> if(isset($ar2[$v['id']])) $v['sign'] = 1;<br />}<br /><br />print_r($ar1);<br />
Array<br />(<br /> [0] => Array<br /> (<br /> [id] => 1<br /> [name] => 张1<br /> [sign] => 1<br /> )<br /><br /> [1] => Array<br /> (<br /> [id] => 2<br /> [name] => 张2<br /> [sign] => 1<br /> )<br /><br /> [2] => Array<br /> (<br /> [id] => 5<br /> [name] => 张5<br /> [sign] => <br /> )<br /><br />)<br /><br />