第一个表的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 ) ) ) )
$table1[array_search($table1[$key]['typeid'], array_column($table2, 'id'))];
//这段代码的意思是如果你的第二个数组里的id能在第一个表里找到。然后你就可以把这个拼接进你的第一个数组了,这过程是在循环里进行的。例: $$table1[$key]['signed']=$table1[array_search($table1[$key]['typeid'], array_column($table2, 'id'))];
看看这个呢http://q.cnblogs.com/q/83435/
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 }
这是比较笨的方式,你试试吧
结果不对呢
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] => ) ) ) )
http://q.cnblogs.com/q/83435/
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; } } } }