二维数组相同数组的字段 合并

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']);
            
          }
         这就像咱们最后想要的数据。
 最后 ,到我们的视图层循环展示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值