


Array array and using foreach loop array graphic example detailed explanation
1. 了解数组
PHP 中的数组实际上是一个有序映射。映射是一种把 values 关联到 keys 的类型。
2.例子:一般的数组
这里,我通过一个简单的例子,并使用图形方式来了解数组。
//1.----------------------------------- $a = array(3 => 'a', 1 => 'b', 2 => 'c'); echo var_dump($a);
[注]:使用箭头描述数组$a各个单元对应某一内存地址的数据值(实际上,它内部结构采用HashTable结构)。
3.例子:在数组定义中,添加引用。
//2.----------------------------------- $x = 'x'; $a = array(3 => 'a', 1 => &$x, 2 => 'c'); echo "<hr>"; echo var_dump($a); $x = 'y'; echo var_dump($a);
数组$a中的第2个单元$a[1]与$x对应的是同一个数据,使用var_dump($a)的时候,会看到数组的第2个单元多个&符号,即&string(1) "x",表示引用。
当 修改$x的值='y',也等同于修改$a[1]的值='y'。
下图可以清楚的描述这一变化:
//3.----------------------------------- $a = array(3 => 'a', 1 => 'b', 2 => 'c'); echo "<hr>"; foreach ($a as $key => $value) { echo "$key => $value <br>"; }
每次循环中,当前数组中单元的值被赋给 $value ,单元的键key被赋给$key。如下图描述:
【注:】灰色虚箭头表示赋予某一值。
5.例子:在foreach遍历数组中,使用引用赋值。
//4.----------------------------------- $a = array(3 => 'a', 1 => 'b', 2 => 'c'); echo "<hr>"; foreach ($a as $key => &$value) { $value.='n'; echo "$key => $value <br>"; }
每次循环中, $value 都指向当前数组中单元的值,再执行“ $value.='n'; ”代码 ,如下图描述:
6.例子:对例子5的进一步分析。
在例子5,foreach遍历数组完成后,不会自动销毁$value变量,与数组$a最后一个单元$a[2]指向同一个数据。
这时候改变$value的值,也就是改变了$a[2]的值。
//5.----------------------------------- $value='m'; echo "<hr>"; echo "\$value=$value <br>"; echo var_dump($a);
实例验证, 数组最后一个元素的 $value 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁。
7.小结
以上例子,只是描述php中的array数组和foreach的一些特性。学习最后,感觉php中的array数组和foreach不同于别的编程语言,不能使用类似C语言的结构分析PHP。
The above is the detailed content of Array array and using foreach loop array graphic example detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
