刚接触php,不知道能否先创建一个带键名的空数组,后期使用时在赋值。因为此数组的键名个数是变化的,不能直接定义数组。
我的想法如下:
...$strkeys= "'tagname'=>,'descr'=>,'unit'=>,";for($w= 1; $w<= $days; $w++){ $strkeys= $strkeys."'".$w."日'=>,";}$strkeys= $strkeys."'Total'=>";$excelres[]= array($strkeys);//print_r($excelres);
首先你创建数组的格式就不对.
$strkeys= ['tagname'=>'', 'descr'=>'','unit'=>''];$days=30;for($w= 1; $w<= $days; $w++){ $strkeys[$w.'日']='';}$strkeys['Total']='';print_r($strkeys);[] 格式是PHP 5.4 及以上版本才有的数组简写格式
$strkeys= array('tagname'=>'','descr'=>'','unit'=>'');$days = 31;for($w= 1; $w<= $days; $w++){ $strkeys[$w.'日'] = '';}$strkeys['Total'] = '';$excelres[]= $strkeys;print_r($excelres);
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号