array(10) { [0]=> array(5) { ["times"]=> string(10) "2016-10-26" ["aid"]=> string(1) "1" ["uid"]=> string(1) "2" ["id"]=> string(1) "2" ["username"]=> string(6) "李四" } [1]=> array(5) { ["times"]=> string(10) "2016-10-26" ["aid"]=> string(1) "1" ["uid"]=> string(1) "6" ["id"]=> string(1) "6" ["username"]=> string(6) "小辉" } [2]=> array(5) { ["times"]=> string(10) "2016-10-25" ["aid"]=> string(1) "1" ["uid"]=> string(1) "7" ["id"]=> string(1) "7" ["username"]=> string(6) "小明" } [3]=> array(5) { ["times"]=> string(10) "2016-10-26" ["aid"]=> string(1) "1" ["uid"]=> string(1) "6" ["id"]=> string(1) "6" ["username"]=> string(6) "小辉" } [4]=> array(5) { ["times"]=> string(10) "2016-10-24" ["aid"]=> string(1) "1" ["uid"]=> string(1) "8" ["id"]=> string(1) "8" ["username"]=> string(6) "小飞" } [5]=> array(5) { ["times"]=> string(10) "2016-10-24" ["aid"]=> string(1) "1" ["uid"]=> string(2) "12" ["id"]=> NULL ["username"]=> NULL } [6]=> array(5) { ["times"]=> string(10) "2016-10-23" ["aid"]=> string(1) "1" ["uid"]=> string(2) "11" ["id"]=> string(2) "11" ["username"]=> string(6) "小小" } [7]=> array(5) { ["times"]=> string(10) "2016-10-26" ["aid"]=> string(1) "1" ["uid"]=> string(2) "14" ["id"]=> string(2) "14" ["username"]=> string(6) "小六" } [8]=> array(5) { ["times"]=> string(10) "2016-10-25" ["aid"]=> string(1) "1" ["uid"]=> string(1) "2" ["id"]=> string(1) "2" ["username"]=> string(6) "李四" } [9]=> array(5) { ["times"]=> string(10) "2016-10-26" ["aid"]=> string(1) "1" ["uid"]=> string(1) "2" ["id"]=> string(1) "2" ["username"]=> string(6) "李四" }} //通过那个字段合并的 这里我是通过times 把我要的username及id合并 $res = array(); foreach($ar as $item) { if(! isset($res[$item['times']])) { $res[$item['times']] = $item; } else{ $res[$item['times']]['username'].= ',' . $item['username']; $res[$item['times']]['uid'].= ',' . $item['uid']; } } $res=array_values($res); //把username,uid数据分割开 $data=$res; foreach($res as $key=>$val){ $res[$key]['username']=explode(',',$val['username']); $res[$key]['uid']=explode(',',$val['uid']); } //把username,uid;重复的值去除掉 $obj=array(); foreach($res as $key=>$val){ $obj[$key]['username']=array_unique($val['username']); $obj[$key]['uid']=array_unique($val['uid']); } //然后放到我们原先的数组内 $da=array(); foreach($data as $k=>$v){ $da[$k]=$v; $da[$k]['users']=implode(',',$obj[$k]['username']); $da[$k]['uid']=implode(',',$obj[$k]['uid']); }这就像咱们最后想要的数据。最后 ,到我们的视图层循环展示
二维数组相同数组的字段 合并
最新推荐文章于 2021-11-20 20:00:00 发布