Home > Backend Development > PHP Tutorial > 帮下忙,这有关问题已经困惑了小弟我好几天了

帮下忙,这有关问题已经困惑了小弟我好几天了

WBOY
Release: 2016-06-13 12:26:23
Original
1073 people have browsed it

帮下忙,这问题已经困惑了我好几天了。
如果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);
Copy after login
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 />
Copy after login

你总得知道自己要做什么的

------解决思路----------------------
$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);
Copy after login


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)
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