我想把PHP的兩個數組放到一起,請大神指教

來源:互聯網
上載者:User
**問題:我想從資料庫中查詢每個會員發布的兩條資訊,並以數組形式封裝

                $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;

}
手機打字回複,有錯別字莫見怪

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.