php foreach循环处理数组
文章描述:
php使用foreach处理数组里面的内容
数据表
栏目表

文章表

查询条件
条件是父id为2的栏目
$where[] = [
    ['b.parent_id', '=', 2]
];查询语句
查询父栏目id为2的所有子栏目
$items = Db::name('category')->alias('b')->field('b.*')->where($where)->select()->all();查询语句
查询所有父级栏目id为2的文章
$items = Db::name('article')->alias('a')->field('a.*,b.cat_name')->join('category b', 'a.cat_id=b.cat_id')->where($where)->select()->all();或者
foreach($items as $k=>$v){
    $items[$k]['list'] = Db::name('article')->alias('a')->field('a.*,b.cat_name')->join('category b', 'a.cat_id=b.cat_id')->where(
        'a.cat_id','=',$v['cat_id']
    )->select()->all();
}循环处理
给所有文章图片添加域名
foreach ($items as $k=>$v){
       foreach ($v['list'] as $key=>$val){
            $img_path = $val['thumb'];
            $v['list'][$key]['thumb'] = 'https://'.$_SERVER['SERVER_NAME'].'/'.str_replace('\\','/',$img_path);
       }
       $items[$k]['list']=$v['list'];
}
发布时间:2021/09/16 
                
            
发表评论