Rumah >pembangunan bahagian belakang >tutorial php >帮下忙,这有关问题已经困惑了小弟我好几天了
帮下忙,这问题已经困惑了我好几天了。
如果GID等于ID就添加新值,请问怎样实现呢?
------解决思路----------------------
$start = array(<br /> array('id' => 11, 'name' => '张三', 'title' => '上班'),<br /> array('id' => 19, 'name' => '李四', 'title' => '没上班'),<br /> array('id' => 18, 'name' => '王五', 'title' => '下班'),<br /> );<br /><br />$saler = array(<br /> array('gid' => 19, 'salernum' => '10', 'num' => '80'),<br /> array('gid' => 18, 'salernum' => '11', 'num' => '81'),<br /> );<br /><br />//格式化 $start<br />foreach($start as $item) $r[$item['id']] = $item;<br /><br />//赋值<br />foreach($saler as $item)<br /> if(isset($r[$item['gid']])) $r[$item['gid']]['num'] = $item['num'];<br /><br />$start = array_values($r);<br />print_r($start);
Array<br />(<br /> [0] => Array<br /> (<br /> [id] => 11<br /> [name] => 张三<br /> [title] => 上班<br /> )<br /><br /> [1] => Array<br /> (<br /> [id] => 19<br /> [name] => 李四<br /> [title] => 没上班<br /> [num] => 80<br /> )<br /><br /> [2] => Array<br /> (<br /> [id] => 18<br /> [name] => 王五<br /> [title] => 下班<br /> [num] => 81<br /> )<br /><br />)<br /><br />
$start = array(<br /> array('id'=>11,'name'=>'张三','title'=>'上班'),<br /> array('id'=>19,'name'=>'李四','title'=>'没上班'),<br /> array('id'=>18,'name'=>'王五','title'=>'下班')<br />);<br />$saler = array(<br /> array('gid'=>19,'salernum'=>'10','num'=>'80'),<br /> array('gid'=>18,'salernum'=>'11','num'=>'81'),<br />);<br /><br />foreach($start as $key=>$val){<br /> foreach($saler as $v){<br /> if($val['id'] == $v['gid']){<br /> $start[$key]['num'] = $v['num'];<br /> break;<br /> }<br /> $start[$key]['num'] = '0';<br /> }<br />}<br />var_dump($start);
array<br /> 0 => <br /> array<br /> 'id' => int 11<br /> 'name' => string '张三' (length=6)<br /> 'title' => string '上班' (length=6)<br /> 'num' => string '0' (length=1)<br /> 1 => <br /> array<br /> 'id' => int 19<br /> 'name' => string '李四' (length=6)<br /> 'title' => string '没上班' (length=9)<br /> 'num' => string '80' (length=2)<br /> 2 => <br /> array<br /> 'id' => int 18<br /> 'name' => string '王五' (length=6)<br /> 'title' => string '下班' (length=6)<br /> 'num' => string '81' (length=2)