Rumah > pembangunan bahagian belakang > tutorial php > 我想把PHP的两个数组放到一起,请大神指教

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

WBOY
Lepaskan: 2016-06-06 20:24:07
asal
1236 orang telah melayarinya

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<code>                $userlist里面是查询出来的所有用户。

                 

                 $listinfo里面是查询出来的这个用户发布的两条数据。

                 我想以这样的形式封装

             一个用户是一个数组,然后用户这个数组里面包含了一个数组,这个数组里面是这个用户发布的两条信息:

            大数组{

                用户数组{

                      用户产品数组{

                              用户产品数组1{}

                              用户产品数组2{}             

                                  }                 

                 }

                                  

            }**

          

</code>

Salin selepas log masuk
Salin selepas log masuk

// 查询会员信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

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

    }</code>

Salin selepas log masuk
Salin selepas log masuk

回复内容:

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<code>                $userlist里面是查询出来的所有用户。

                 

                 $listinfo里面是查询出来的这个用户发布的两条数据。

                 我想以这样的形式封装

             一个用户是一个数组,然后用户这个数组里面包含了一个数组,这个数组里面是这个用户发布的两条信息:

            大数组{

                用户数组{

                      用户产品数组{

                              用户产品数组1{}

                              用户产品数组2{}             

                                  }                 

                 }

                                  

            }**

          

</code>

Salin selepas log masuk
Salin selepas log masuk

// 查询会员信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

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

    }</code>

Salin selepas log masuk
Salin selepas log masuk

稍微改动一下就好了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

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

}</code>

Salin selepas log masuk

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

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

1

2

<code>  $b = "用户发布的信息";

 $userinfo[$key]["message"] = $b;</code>

Salin selepas log masuk

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan