当前位置:Gxlcms > PHP教程 > 我想把PHP的两个数组放到一起,请大神指教

我想把PHP的两个数组放到一起,请大神指教

时间:2021-07-01 10:21:17 帮助过:6人阅读

**问题:我想从数据库中查询每个会员发布的两条信息,并以数组形式封装

                $userlist里面是查询出来的所有用户。
                
                 $listinfo里面是查询出来的这个用户发布的两条数据。
                 我想以这样的形式封装
             一个用户是一个数组,然后用户这个数组里面包含了一个数组,这个数组里面是这个用户发布的两条信息:
            大数组{
                用户数组{
                      用户产品数组{
                              用户产品数组1{}
                              用户产品数组2{}              
                                  }                  
                 }
                                 
            }**
         

// 查询会员信息

    $accompanyinfo = $this->db
                 ->from($this->db->dbprefix('member').' AS m2')
                 ->join($this->db->dbprefix('member_data').' AS a', 'a.uid=m2.uid', 'left')
                 ->where('a.service<>', '')
                 ->where('a.uid<>',1)
                 ->limit($start,$limit)
                 ->get()
                 ->result_array();
                 $userlist=array();//用户
                 $listinfo=array();//产品
         
    foreach ($accompanyinfo as  $t) {
            $a['uid'] = $t['uid'];
            $a['name']=$t['name'];
            $a['sex']=$t['sex'];
            $a['age']=$t['age'];
            $a['status'] = unserialize(stripslashes($t['status']));
            $a['service'] = unserialize(stripslashes($t['service']));
            $a['tx']=SYS_ATTACHMENT_URL.'member/'.$t['uid'].'/'.$t['txdz'].'.jpg';
            $a['hobby'] = $t['hobby'];
            $a['contact'] = $t['contact'];
            $a['abstract'] = $t['abstract'];
            $a['seat']=dr_linkagepos('address',$t['seat'],'', NULL);
            //查询两条产品
        $list = $this->db
                 ->where('uid', $a['uid'])
                 ->where('catid',1)
                 ->limit(2)
                 ->order_by('inputtime DESC')
                 ->get('dr_1_accompany')
                 ->result_array();
      
        foreach ($list as $s) {
           $b['id']=$s['id'];//信息ID
           $b['uid']=$s['uid'];//发布人ID
           $b['catid']=$s['catid'];//栏目ID
           $b['title']=$s['title'];//标题
           $b['fuwudidian']=dr_linkagepos('address',$s['fuwudidian'],'-', NULL);//服务地点
           $b['peihuxingshi']=$s['peihuxingshi'];//陪护形式
           $b['chujia']=$s['chujia'];//出价
           $b['tese'] = unserialize(stripslashes($s['tese']));//特色
           $b['teshufuwu']=$s['teshufuwu'];//特殊服务
           $listinfo[] = $b;
        }
         
         $userlist[] = $a;
    }

回复内容:

**问题:我想从数据库中查询每个会员发布的两条信息,并以数组形式封装

                $userlist里面是查询出来的所有用户。
                
                 $listinfo里面是查询出来的这个用户发布的两条数据。
                 我想以这样的形式封装
             一个用户是一个数组,然后用户这个数组里面包含了一个数组,这个数组里面是这个用户发布的两条信息:
            大数组{
                用户数组{
                      用户产品数组{
                              用户产品数组1{}
                              用户产品数组2{}              
                                  }                  
                 }
                                 
            }**
         

// 查询会员信息

    $accompanyinfo = $this->db
                 ->from($this->db->dbprefix('member').' AS m2')
                 ->join($this->db->dbprefix('member_data').' AS a', 'a.uid=m2.uid', 'left')
                 ->where('a.service<>', '')
                 ->where('a.uid<>',1)
                 ->limit($start,$limit)
                 ->get()
                 ->result_array();
                 $userlist=array();//用户
                 $listinfo=array();//产品
         
    foreach ($accompanyinfo as  $t) {
            $a['uid'] = $t['uid'];
            $a['name']=$t['name'];
            $a['sex']=$t['sex'];
            $a['age']=$t['age'];
            $a['status'] = unserialize(stripslashes($t['status']));
            $a['service'] = unserialize(stripslashes($t['service']));
            $a['tx']=SYS_ATTACHMENT_URL.'member/'.$t['uid'].'/'.$t['txdz'].'.jpg';
            $a['hobby'] = $t['hobby'];
            $a['contact'] = $t['contact'];
            $a['abstract'] = $t['abstract'];
            $a['seat']=dr_linkagepos('address',$t['seat'],'', NULL);
            //查询两条产品
        $list = $this->db
                 ->where('uid', $a['uid'])
                 ->where('catid',1)
                 ->limit(2)
                 ->order_by('inputtime DESC')
                 ->get('dr_1_accompany')
                 ->result_array();
      
        foreach ($list as $s) {
           $b['id']=$s['id'];//信息ID
           $b['uid']=$s['uid'];//发布人ID
           $b['catid']=$s['catid'];//栏目ID
           $b['title']=$s['title'];//标题
           $b['fuwudidian']=dr_linkagepos('address',$s['fuwudidian'],'-', NULL);//服务地点
           $b['peihuxingshi']=$s['peihuxingshi'];//陪护形式
           $b['chujia']=$s['chujia'];//出价
           $b['tese'] = unserialize(stripslashes($s['tese']));//特色
           $b['teshufuwu']=$s['teshufuwu'];//特殊服务
           $listinfo[] = $b;
        }
         
         $userlist[] = $a;
    }

稍微改动一下就好了

$accompanyinfo = $this->db
                 ->from($this->db->dbprefix('member').' AS m2')
                 ->join($this->db->dbprefix('member_data').' AS a', 'a.uid=m2.uid', 'left')
                 ->where('a.service<>', '')
                 ->where('a.uid<>',1)
                 ->limit($start,$limit)
                 ->get()
                 ->result_array();         
$userlist=array();             
     
foreach ($accompanyinfo as  $t) {
    $a['uid'] = $t['uid'];
    $a['name']=$t['name'];
    $a['sex']=$t['sex'];
    $a['age']=$t['age'];
    $a['status'] = unserialize(stripslashes($t['status']));
    $a['service'] = unserialize(stripslashes($t['service']));
    $a['tx']=SYS_ATTACHMENT_URL.'member/'.$t['uid'].'/'.$t['txdz'].'.jpg';
    $a['hobby'] = $t['hobby'];
    $a['contact'] = $t['contact'];
    $a['abstract'] = $t['abstract'];
    $a['seat']=dr_linkagepos('address',$t['seat'],'', NULL);
    //查询两条产品
    $list = $this->db
             ->where('uid', $a['uid'])
             ->where('catid',1)
             ->limit(2)
             ->order_by('inputtime DESC')
             ->get('dr_1_accompany')
             ->result_array();
    $listinfo = array(); //在这里要初始化
    foreach ($list as $s) {
            $b['id']=$s['id'];//信息ID
            $b['uid']=$s['uid'];//发布人ID
            $b['catid']=$s['catid'];//栏目ID
            $b['title']=$s['title'];//标题
            $b['fuwudidian']=dr_linkagepos('address',$s['fuwudidian'],'-', NULL);//服务地点
            $b['peihuxingshi']=$s['peihuxingshi'];//陪护形式
            $b['chujia']=$s['chujia'];//出价
            $b['tese'] = unserialize(stripslashes($s['tese']));//特色
            $b['teshufuwu']=$s['teshufuwu'];//特殊服务
            $listinfo[] = $b;
        }
    $a['listinfo'] = $listinfo; //
    $userlist[] = $a;
}

试试 $a['listinfo'] = $listinfo;
没仔细看,理解的$a是当前用户的个人资料,$listinfo是当前用户发布信息的数组

你可以在循环$userinfo的时候获取用户发布信息,然后把获取的信息追加到数组的一个自定义项中,foreach($userinfo as $key=> $a){

  $b = "用户发布的信息";
 $userinfo[$key]["message"] = $b;

}
手机打字回复,有错别字莫见怪

人气教程排行