php 数组遍历(for、foreach)

php 遍历的几种方法,
for、foreach
1,for

2.foreach
1)省略索引
php 数组遍历(for、foreach)_第1张图片

$usergroup = json_decode($data['usergroup'], true);
            //为每个用户生成一条记录
            foreach ($usergroup as $v) {
            //$v就是 $usergroup数组里的每条数据(每个UserID)
                $update['UserID'] = $v['UserID'];
                $update['Delect'] = 0;
                $update['Type'] = 1;
                $update['Title'] = $data['title'];
                $update['Content'] = $data['emailcontent'];
                $update['Appendix'] = $data['awardprop'];
                $update['State'] = 0;
                $update['CreateDateTime'] = $data['auditdatetime'];
                $update['Sender'] = 0;
                $res = M('gamemailuserinfo', null, 'C_GameUserDB')
                    ->add($update);
                if ($res === false) {
                    $this->error('数据库异常');
                }
            }

简写foreach
一维数组
1)不带索引

foreach($arr as $value){

}

2)带索引

foreach($arr as $index=>$value){

}

二维数组

foreach ($arr as $value) {
                foreach ($value as $v) {
......
                }
            }

引用:https://www.cnblogs.com/waj6511988/p/6927208.html

你可能感兴趣的:(php)