概述
结果1:array[1,2,45]
一、循环记录方法
代码:
$rs = Db::name('register')
->alias('r')
->field('r.id,r.user_id')
->join('register_class c','r.class_id = c.id')//模块
->where('r.deleted',0)
->where('c.deleted',0)
->select();
if(count($rs)>0){
$useridarr = [];
foreach($rs as $key => $v){
$useridarr[]=$v['user_id'];
}
}
$useridarr[]=$v['user_id'];
结果2:array[{id:1,titl:标题1},{id:2,titl:标题2}]
foreach($cidarr as $cid){
$rs4=Db::name('xx')->field('id,title')->where('deleted',0)->select();
if(count($rs4)>0){
foreach($rs4 as $k=> $v4){
$uidarr[] = $v4['id'];
$arr['id'] = $v4['id'];
$arr['title'] = $v4['title'];
$uidtitlearr[] = $arr;
}
}
}
$uidarr输出:array[1,2,45]
$uidtitlearr输出:array[{id:1,titl:标题1},{id:2,titl:标题2}]
二、php内置函数实现
$rs = Db::name('register')
->alias('r')
->field('r.id,r.user_id')
->join('register_class c','r.class_id = c.id')//模块
->where('r.deleted',0)
->where('c.deleted',0)
->select();
if(count($rs)>0){
// foreach($rs as $key => $v1){
// $useridarr[]=$v1['user_id'];
// }
$useridarr = array_column($rs,'user_id');
}
$useridarr = array_column($rs,'user_id');
array_column($rs,'user_id'):从记录中取得user_id的值组成数组
其它例子:
$useridarr = []; 在开始前一定要设置这个
if(count($cidarr)){
$useridarr = [];
foreach($cidarr as $k => $cid){
//dump($cid);
$rs = Db::name('register')
->alias('r')
->field('r.id,r.user_id')
->join('register_class c','r.class_id = c.id')//模块
->where('r.class_id',$cid)
->where('r.deleted',0)
->where('r.state_copy',0)
->where('c.deleted',0)
->select();
if(count($rs)>0){
// foreach($rs as $key => $v1){
// $useridarr[]=$v1['user_id'];
// }
$useridarr = array_merge($useridarr,array_column($rs,'user_id'));
}
//dump($useridarr);
}
//dump($useridarr);
$useridarr = array_values(array_unique($useridarr));
}
$useridarr = array_merge($useridarr,array_column($rs,'user_id'));
array_column($rs,'user_id'):从记录中取得user_id的值组成数组
array_merge($useridarr,array_column($rs,'user_id')):合拼数组($useridarr)
php数组合并 要保持两个数组的键名都不变
https://blog.csdn.net/haibo0668/article/details/118421962
最后
以上就是紧张诺言为你收集整理的tp5.1 php 记录集foreach for循环array_merge函数合拼、array_column函数组装成一维数组array[1,2,45]结果1:array[1,2,45]一、循环记录方法$useridarr[]=$v['user_id'];结果2:array[{id:1,titl:标题1},{id:2,titl:标题2}]二、php内置函数实现其它例子:php数组合并 要保持两个数组的键名都不变的全部内容,希望文章能够帮你解决tp5.1 php 记录集foreach for循环array_merge函数合拼、array_column函数组装成一维数组array[1,2,45]结果1:array[1,2,45]一、循环记录方法$useridarr[]=$v['user_id'];结果2:array[{id:1,titl:标题1},{id:2,titl:标题2}]二、php内置函数实现其它例子:php数组合并 要保持两个数组的键名都不变所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复