"Zhang1","sign"=>""),array('id'=>2,"name"= > "張 2","符号"=>""),array('id'=>5,"名前"=>"張 5","符号"=>""));"/> "Zhang1","sign"=>""),array('id'=>2,"name"= > "張 2","符号"=>""),array('id'=>5,"名前"=>"張 5","符号"=>""));">
ホームページ >バックエンド開発 >PHPチュートリアル >PHP 配列比較関連の問題
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 />