刚接触php,不知道能否先创建一个带键名的空数组,后期使用时在赋值。因为此数组的键名个数是变化的,不能直接定义数组。
我的想法如下:
...$strkeys= "'tagname'=>,'descr'=>,'unit'=>,";for($w= 1; $w<= $days; $w++){ $strkeys= $strkeys."'".$w."日'=>,";}$strkeys= $strkeys."'Total'=>";$excelres[]= array($strkeys);//print_r($excelres);
但使用print_r($excelres);后得到结果和想的不一样,如下:
Array ( [0] => Array ( [0] => 'tagname'=>,'descr'=>,'unit'=>,'1日'=>,'2日'=>,'3日'=>,'4日'=>,'5日'=>,'6日'=>,'7日'=>,'8日'=>,'9日'=>,'10日'=>,'11日'=>,'12日'=>,'13日'=>,'14日'=>,'15日'=>,'16日'=>,'17日'=>,'18日'=>,'19日'=>,'20日'=>,'21日'=>,'22日'=>,'23日'=>,'24日'=>,'25日'=>,'26日'=>,'27日'=>,'28日'=>,'29日'=>,'30日'=>,'Total'=> ) [1] => Array ( [0] => 'tagname'=>,'descr'=>,'unit'=>,'1日'=>,'2日'=>,'3日'=>,'4日'=>,'5日'=>,'6日'=>,'7日'=>,'8日'=>,'9日'=>,'10日'=>,'11日'=>,'12日'=>,'13日'=>,'14日'=>,'15日'=>,'16日'=>,'17日'=>,'18日'=>,'19日'=>,'20日'=>,'21日'=>,'22日'=>,'23日'=>,'24日'=>,'25日'=>,'26日'=>,'27日'=>,'28日'=>,'29日'=>,'30日'=>,'Total'=> ) )
回复讨论(解决方案)
首先你创建数组的格式就不对.
$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);
Array
(
[0] => Array
(
[tagname] =>
[descr] =>
[unit] =>
[1日] =>
[2日] =>
[3日] =>
[4日] =>
[5日] =>
[6日] =>
[7日] =>
[8日] =>
[9日] =>
[10日] =>
[11日] =>
[12日] =>
[13日] =>
[14日] =>
[15日] =>
[16日] =>
[17日] =>
[18日] =>
[19日] =>
[20日] =>
[21日] =>
[22日] =>
[23日] =>
[24日] =>
[25日] =>
[26日] =>
[27日] =>
[28日] =>
[29日] =>
[30日] =>
[31日] =>
[Total] =>
)
)

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
