Home  >  Q&A  >  body text

PHP array transformation and assembly

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]=>
    NULL
    [11]=>
    NULL
    [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
  }
}

The above array is read from the database
The above year is the year, 01 to 12 is January to December, and then, 16, 17, 25 is the date

How to make it in the following format! God help me

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 represents the year, 04 represents the month, 15, and 20 represents the date of 15th and 20th

曾经蜡笔没有小新曾经蜡笔没有小新2712 days ago687

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:18:39

    $a = array(

            array(
                'year'=>'2017',
                'one'=>',16,17,25,27,18,27',
                'two'=>'',
                'three'=>',16,17,25,27,18,19,15,24'
            ),
            array(
                'year'=>'2018',
                'one'=>'',
                'two'=>',16,17,25,27,18,27',
                'three'=>',16,17,25,27,18,19,15,24'
                ),
            );
    
        foreach($a as $key => $value){
            $year = $value['year'];
            unset($value['year']);
            foreach($value as $k => $v){
                $result = explode(',',substr($v,1));
                $time[$year][$k] = $result;
            }
        }
        
        var_dump($time);
        
        最上面还有一行 $a = array(  显示不了··

    reply
    0
  • Cancelreply