search
Homephp教程php手册PHP数组读取的循环操作

PHP基本上就是一种数组语言。时常要进行大量的数组循环操作,主要有两种方式,一种是foreach,另一种是while,到底哪种好哪种坏一直有争论,虽然我很早就意识到了这个问题,但是一直没有细究,懵懂的感觉一直持续到现在,为了以后能节省点CPU时间,下面总结

    PHP基本上就是一种数组语言。时常要进行大量的数组循环操作,主要有两种方式,一种是foreach,另一种是while,到底哪种好哪种坏一直有争论,虽然我很早就意识到了这个问题,但是一直没有细究,懵懂的感觉一直持续到现在,为了以后能节省点CPU时间,下面总结一下:

    在循环里进行的是数组“读”操作,则foreach比while快:

    无格式查看复制到剪贴板打印代码?

foreach ($array as $value) {
    echo $value;
}
while (list($key) = each($array)) {
    echo $array[$key];
}
foreach ($array as $value) {
    echo $value;
}
while (list($key) = each($array)) {
    echo $array[$key];
}
    在循环里进行的是数组“写”操作,则while比foreach快:

    无格式查看复制到剪贴板打印代码?

foreach ($array as $key => $value) {
    echo $array[$key] = $value . '...';
}
while (list($key) = each($array)) {
    $array[$key] = $array[$key] . '...';
}
foreach ($array as $key => $value) {
    echo $array[$key] = $value . '...';
}
while (list($key) = each($array)) {
    $array[$key] = $array[$key] . '...';
}
    总结:通常认为,foreach涉及到值复制,一定会比while慢,但实际上,如果仅仅是在循环里进行数组的读操作,那么foreach是很快的,这是因为PHP采用的复制机制是“引用复制,写时拷贝”,这样看来,foreach的高效读操作就不难理解了。

    另外,既然foreach不适合处理数组写操作,那么我们可以得出一个结论,多数情况下,类似foreach ($array as $key => $value)形式的代码都应该被替换成while (list($key) = each($array))。

    这些技巧产生的速度差异在小项目里可能并不明显,但是在类似框架这样的大项目中,一次请求动辄便会涉及到几百几千几万次数组循环操作,差异就会明显放大。

 

PHP数组读取的循环操作

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use