


PHP--Usage of each and list list for each safe each list as value i js traverse list collection eac
1.Usage of each
Let’s look at the API first
array each ( array &$array )
This is described in the API: each — returns the current key/value pair in the array and moves the array pointer forward one step
Let’s first take a look at what the returned array looks like?
<code><span><?php </span><span>$arr</span> = <span>array</span>(<span>'你'</span>,<span>'若'</span>,<span>'安'</span>,<span>'好'</span>,<span>'便'</span>,<span>'是'</span>,<span>'晴'</span>,<span>'天'</span>); print_r(each(<span>$arr</span>)); print_r(each(<span>$arr</span>)); <span>echo</span><span>'<hr>'</span>; <span>/* 返回 Array ( [1] => 你 [value] => 你 [0] => 0 [key] => 0 ) Array ( [1] => 若 [value] => 若 [0] => 1 [key] => 1 ) */</span><span>//执行相同的一段代码,从‘你’到‘若’,说明each是会每执行一次,游标向数组尾部移动一步</span><span>//0和Key存放的是键</span><span>//1和value存放的是值</span><span>//因此each满足遍历数组的,得到当前的键和值,以及每执行一次,向尾部移动一步游标</span><span>//因此循环数组也可以用each这么写</span> reset(<span>$arr</span>); <span>for</span>(;<span>$tmp</span>=each(<span>$arr</span>);){ <span>echo</span><span>$tmp</span>[<span>0</span>],<span>'~'</span>,<span>$tmp</span>[<span>1</span>],<span>'<br>'</span>; } <span>/* 返回 0~你 1~若 2~安 3~好 4~便 5~是 6~晴 7~天 */</span><span>?></span></span></code>
2.list usage
Let’s first see what the API says
Like array(), this is not a real function, but a language construct. list() assigns values to a set of variables in one step.
Let’s look at an example:
<code><span><?php </span><span>list</span>(<span>$a</span>,<span>$b</span>)=<span>array</span>(<span>10</span>,<span>20</span>); <span>echo</span><span>$a</span>,<span>'~'</span>,<span>$b</span>,<span>'<br>'</span>; <span>//返回10~20</span><span>?></span></span></code>
Yes, you can assign values to a set of variables
Let’s look at another example:
<code><span><?php </span><span>list</span>(<span>$a</span>,<span>$b</span>,,<span>$c</span>)=<span>array</span>(<span>2</span>=><span>10</span>,<span>3</span>=><span>20</span>,<span>4</span>=><span>30</span>,<span>1</span>=><span>40</span>); <span>echo</span><span>$a</span>,<span>'~'</span>,<span>$b</span>,<span>'~'</span>,<span>$c</span>,<span>'<br>'</span>; <span>//返回notice~40~20</span><span>//执行到$a的时候返回给我一个notice:说数组没有0键</span><span>?></span></span></code>
According to the general idea, it should return: 10~20~40
Why is this notice~40~20 returned?
Answer: This involves the operating mechanism of the list. This is how the list is assigned
First of all: ignore the array on the right and look at the variables in the List. From left to right it should be
So
3. Use each and list to implement array traversal
<code><span><?php </span><span>$arr</span> = <span>array</span>(<span>'你'</span>,<span>'若'</span>,<span>'安'</span>,<span>'好'</span>,<span>'便'</span>,<span>'是'</span>,<span>'晴'</span>,<span>'天'</span>); <span>for</span>(;<span>list</span>(<span>$k</span>,<span>$v</span>)=each(<span>$arr</span>);){ <span>echo</span><span>$k</span>,<span>'~'</span>,<span>$v</span>,<span>'<br>'</span>; } <span>/* return: 0~你 1~若 2~安 3~好 4~便 5~是 6~晴 7~天 */</span><span>?></span></span></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above introduces the usage of PHP--each and list, including the content of each and list. I hope it will be helpful to friends who are interested in PHP tutorials.

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
