I want to teach php how to create an empty array with a key name, and assign values to php later. I don't know if I can create an empty array with a key name, and assign values to it later. Because the number of key names of this array changes, you cannot directly define an array.
My ideas are as follows:
... $ Strkeys = "'tagname' =>, 'desc' =>, 'unit '=>,"; for ($ w = 1; $ w <= $ days; $ w ++) {$ strkeys = $ strkeys. "'". $ w. "Day '=>," ;}$ strkeys = $ strkeys. "'total' =>"; $ excelres [] = array ($ strkeys); // print_r ($ excelres );
However, with print_r ($ excelres);, the result is different from the expected one, as shown below:
Array ([0] => Array ([0] => 'tagname' =>, 'descal' =>, 'unit '=>, '1day' =>, '2 day' =>, '3 day' =>, '4 day' =>, '5 day' =>, '6 day' =>, '7' =>, '8' =>, '9' =>, '10' =>, '11' =>, '12' =>, '13th '=>, '14th' =>, '15th '=>, '16th' =>, '17' =>, '18 '=>, '19' =>, '20 '=>, '21' =>, '22 day' =>, '23 day' =>, '24 day' =>, '25 day' =>, '26 day' =>, '27' =>, '28' =>, '29' =>, '30' =>, 'total' =>) [1] => Array ([0] => 'tagname' =>, 'descal' =>, 'unit '=>, '1day' =>, '2 day' =>, '3 day' =>, '4 day' =>, '5 day' =>, '6 day' =>, '7' =>, '8' =>, '9' =>, '10' =>, '11' =>, '12' =>, '13th '=>, '14th' =>, '15th '=>, '16th' =>, '17' =>, '18 '=>, '19' =>, '20 '=>, '21' =>, '22 day' =>, '23 day' =>, '24 day' =>, '25 day' =>, '26 day' =>, '27' =>, '28' =>, '29' =>, '30' =>, 'total' => ))
Reply to discussion (solution)
First, the format of the created array is incorrect.
$ Strkeys = ['tagname' => '', 'descal' =>'', 'unit '=> '']; $ days = 30; for ($ w = 1; $ w <= $ days; $ w ++) {$ strkeys [$ w. 'day'] = '';} $ strkeys ['total'] =''; print_r ($ strkeys );
[] The format is the abbreviated format of arrays available only in PHP 5.4 and later versions.
$ Strkeys = array ('tagname' => '', 'descal' =>'', 'unit '=> ''); $ days = 31; for ($ w = 1; $ w <= $ days; $ w ++) {$ strkeys [$ w. 'day'] = '';} $ strkeys ['total'] =''; $ excelres [] = $ strkeys; print_r ($ excelres );
Array
(
[0] => Array
(
[Tagname] =>
[Descr] =>
[Unit] =>
[1 day] =>
[2] =>
[3] =>
[4 days] =>
[5 days] =>
[6] =>
[7 days] =>
[October 8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13th] =>
[14th] =>
[15th] =>
[16] =>
[17th] =>
[18th] =>
[19] =>
[20] =>
[21st] =>
[22nd] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29th] =>
[30] =>
[31st] =>
[Total] =>
)
)