Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

php三维数组改造大神进

array(2) {
  [0]=>
  array(15) {
    ["t_id"]=>
    string(2) "87"
    ["b_id"]=>
    string(2) "21"
    ["year"]=>
    string(4) "2018"
    ["01"]=>
    string(6) ",09,16"
    ["02"]=>
    NULL
    ["03"]=>
    NULL
    ["04"]=>
    NULL
    ["05"]=>
    NULL
    ["06"]=>
    NULL
    ["07"]=>
    NULL
    ["08"]=>
    NULL
    ["09"]=>
    NULL
   ["10"]=>
    string(6) ",18,18"
    ["11"]=>
    string(3) ",09"
    ["12"]=>
    NULL
  }
  [1]=>
  array(15) {
    ["t_id"]=>
    string(2) "86"
    ["b_id"]=>
    string(2) "21"
    ["year"]=>
    string(4) "2017"
    ["01"]=>
    NULL
    ["02"]=>
    NULL
    ["03"]=>
    NULL
    ["04"]=>
    string(0) ""
    ["05"]=>
    string(15) ",23,23,24,24,23"
    ["06"]=>
    string(9) ",20,12,15"
    ["07"]=>
    string(6) ",19,10"
    ["08"]=>
    string(0) ""
    ["09"]=>
    string(0) ""
    ["10"]=>
    string(6) ",18,18"
    ["11"]=>
    string(3) ",09"
    ["12"]=>
    NULL
  }
}
上面的数组是从数据库读出来的上面的year是年份 01 到 12是一月到十二月 然后,16,17,25 是日期

怎么弄成 下面的那种格式的尼!大神帮帮忙

array(
      '2017' => array(
      '04'=>array('15','20'),
      '05'=>array('05','18'),
      '11'=>array('11','19'),
     ),
    '2018' => array(
      '03'=>array('15','20'),
      '06'=>array('05','18'),
      '11'=>array('11','19'),
     ),
2017表示年份 04表示月份 15,20表示日期15日 20日

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$monthRs = [];
$monthCfg = ["01","02","03","04","05","06","07","08","09","10","11","12"];
foreach($arr as $key => $val){
    
    foreach($val as $k=>$v){
        $year = $v['year'];
        if(in_array($k,$monthCfg)){
            $monthRs[$k] = explode(',',$v);
        }
        
    }
    
    $temp = [$year=>$monthRs];
    $monthRs = array_merge($monthRs,$temp);
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...