首页 新闻 会员 周边 捐助

PHP连表查询组合数组问题

0
悬赏园豆:10 [已关闭问题] 关闭于 2016-06-21 13:15

第一个表的typeid对应第二个表的id,time是对应表1的signedtime,typeid相同的合并,怎么才能得到这样的结果:

Array
(
    [0] => Array
        (
            [activity_id] => 2
            [name] => 王
            [mobile] => 
            [signed] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 一次签到
                            [create_time] => 1465181547
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                            [time] =>1463056434
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 二次签到
                            [create_time] => 1465182225
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                            [time] =>1463074455
                        )

                    [2] => Array
                        (
                            [id] => 4
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 三次签到
                            [create_time] => 1465189536
                            [listorder] => 0
                            [sign_img] => 
                            [type] =>
                            [time]=> 
                        )            

                )

        )

    [1] => Array
        (
            [activity_id] => 2
            [name] => zhang
            [mobile] => 
            [signed] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 一次签到
                            [create_time] => 1465181547
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                            [time] =>
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 二次签到
                            [create_time] => 1465182225
                            [listorder] => 0
                            [sign_img] => 
                            [type] =>
                            [time] =>
                        )

                    [2] => Array
                        (
                            [id] => 4
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 三次签到
                            [create_time] => 1465189536
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                            [time]=>1463036123
                        )

                   
                )

        )

)
幻月无痕的主页 幻月无痕 | 初学一级 | 园豆:105
提问于:2016-06-20 16:57
< >
分享
所有回答(3)
0
$table1[array_search($table1[$key]['typeid'], array_column($table2, 'id'))];
//这段代码的意思是如果你的第二个数组里的id能在第一个表里找到。然后你就可以把这个拼接进你的第一个数组了,这过程是在循环里进行的。例: $
$table1[$key]['signed']=$table1[array_search($table1[$key]['typeid'], array_column($table2, 'id'))];

 

大楚打码人 | 园豆:4313 (老鸟四级) | 2016-06-20 17:22

看看这个呢http://q.cnblogs.com/q/83435/

支持(0) 反对(0) 幻月无痕 | 园豆:105 (初学一级) | 2016-06-20 17:34
0
1 foreach($arr1 as $key=>$val){
2   
3      foreach($arr2 as $k=>$v){
4           if($v['typeid'] == $val['id']) {
5                $arr1[$key]['child'][] = $v;
6           }
7     }          
8 
9 }        

这是比较笨的方式,你试试吧

Jarvix_xzh | 园豆:204 (菜鸟二级) | 2016-06-20 17:25

结果不对呢

Array
(
    [0] => Array
        (
            [id] => 5
            [activity_id] => 2
            [applyid] => 5
            [signedtime] => 1463056434
            [typeid] => 1
            [name] => 王
            [mobile] => 
            [signed] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 一次签到
                            [create_time] => 1465181547
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 二次签到
                            [create_time] => 1465182225
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                    [2] => Array
                        (
                            [id] => 4
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 三次签到
                            [create_time] => 1465189536
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                )

        )

    [1] => Array
        (
            [id] => 8
            [activity_id] => 2
            [applyid] => 5
            [signedtime] => 1463074455
            [typeid] => 3
            [name] => 王
            [mobile] => 
            [signed] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 一次签到
                            [create_time] => 1465181547
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 二次签到
                            [create_time] => 1465182225
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                    [2] => Array
                        (
                            [id] => 4
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 三次签到
                            [create_time] => 1465189536
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                )

        )

    [2] => Array
        (
            [id] => 16
            [activity_id] => 2
            [applyid] => 6
            [signedtime] => 1463036123
            [typeid] => 4
            [name] => zhang
            [mobile] => 
            [signed] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 一次签到
                            [create_time] => 1465181547
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 二次签到
                            [create_time] => 1465182225
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                    [2] => Array
                        (
                            [id] => 4
                            [activityid] => 2
                            [activityname] => 测试数据2多次签到
                            [signedname] => 三次签到
                            [create_time] => 1465189536
                            [listorder] => 0
                            [sign_img] => 
                            [type] => 
                        )

                )

        )

)
支持(0) 反对(0) 幻月无痕 | 园豆:105 (初学一级) | 2016-06-20 17:32

http://q.cnblogs.com/q/83435/

支持(0) 反对(0) 幻月无痕 | 园豆:105 (初学一级) | 2016-06-20 17:34
0
 for($i=0;$i<count($new_rst);$i++){
            for($j=0;$j<count($new_rst[$i]['signed']);$j++){
                for($k=0;$k<count($result);$k++){
                    if($new_rst[$i]['signed'][$j]['id']==$result[$k]['typeid'] && $new_rst[$i]['name']==$result[$k]['name']){
                        $new_rst[$i]['signed'][$j]['time']=$result[$k]['signedtime'];
                        break;
                    }
                }
            }
        }   

 

幻月无痕 | 园豆:105 (初学一级) | 2016-06-21 13:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册