How to create an empty array with a key name in php and assign a value later

Source: Internet
Author: User
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] =>
)

)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.