Table of Contents
语言基础
问题研究
问题一: 如下代码运行结果为何不是 2/4/6 ?
问题二: 如下代码运行结果为何不是 0=>a 1=>b 2=>c
PHP7新特性之foreach
Home Backend Development PHP Tutorial An in-depth analysis of foreach in PHP

An in-depth analysis of foreach in PHP

May 31, 2021 pm 07:26 PM
foreach php

本篇文章带大家详解PHP中的foreach。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

An in-depth analysis of foreach in PHP

语言基础

foreach 语法结构提供了遍历数组的简单方式。

php5之前, foreach仅能用于数组 php5+, 利用foreach可以遍历对象

foreach仅能够应用于数据和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。

有两种语法:

/*
  遍历给定的 array_expression 数据。每次循环中, 当前单元的值被赋给$value并且数组内部的指针向前移一步(因此下次循环中将会得到下一个单元)
*/
foreach (array_expression as $value) {
    // statement
}

foreach (array_expression as $value) :
    // statement
endforeach;
Copy after login
/*
  同上,只除了当前单元格的键名也会在每次循环中被赋给变量$key
*/
foreach (array_expression as $key => $value) {
    // statement
}

foreach (array_expression as $key => $value) :
    // statement
endforeach;
Copy after login

还能够自定义遍历对象!

foreach开始执行时, 数组内部的指针会自动指向第一个单元. 这意味着不需要在foreach循环之前调用reset()由于foreach依赖内部数组指针, 在循环中修改其值将可能导致意外的行为

可以很容易通过在 $value 之前加上 & 来修改数组元素. 此方法将以引用 赋值, 而不是拷贝一个值.

<?php

$arr = [1, 2, 3, 4];
foreach($arr as &$value) {
    $value = $value * 2;
}

// $arr is now [2, 4, 6, 8]
unset($value); // 最后取消掉引用
Copy after login

$value的引用仅在被遍历的数组可以被引用时才可用(例如是个变量)。

以下代码无法运行:

<?php
/*
  此段代码可以运行
  运行结果:
    1-2
    2-4
    3-6
    4-8
*/
foreach (array(1, 2, 3, 4) as &$value) {
    echo $value, &#39;-&#39;;
    $value = $value * 2;
    echo $value, PHP_EOL;
}
Copy after login

Warning: 数组最后一个元素的 $value 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁。

Note: foreach 不支持用 @ 来抑制错误信息的能力

foreach 虽然简单, 不过它可能出现一些意外行为, 特别是代码涉及到引用的时候。

问题研究

问题一: 如下代码运行结果为何不是 2/4/6 ?

<?php
$arr = [1, 2, 3];

foreach ($arr as $k => &$v) {
    $v = $v * 2;
}

foreach ($arr as $k => $v) {
    echo $v, PHP_EOL;
}

/*
输出:
    2
    4
    4
*/
Copy after login

我们可以认为 foreach($arr as &$v) 结构隐含了如下操作, 分别将数组当前的 赋值给 $k$v. 具体展开形如:

<?php
foreach ($arr as $k => $v) {
    $k = currentKey();
    $v = currentVal();
    // 继续运行用户代码
}
Copy after login

根据上述理论, 现在我们重新来分析下第一个foreach:

循环备注$arr值
循环 1-1由于$v是一个引用, 因此 $v = &$arr[0], $v = $v * 2 相当于 $arr[0] * 2[2, 2, 3]
循环 1-2$v = &$arr[1][2, 4, 3]
循环 1-3$v = &$arr[2][2, 4, 6]
循环 2-1隐含操作 $v = $arr[0] 被触发, 由于此时 $v 仍是 $arr[2] 的引用, 相当于 $arr[2] = $arr[0][2, 4, 2]
循环 2-2$v = $arr[1], 即$arr[2] = $arr[1][2, 4, 4]
循环 2-3$v = $arr[2], 即$arr[2] = $arr[2][2, 4, 4]

如何解决此类问题呢? PHP手册上有一段提醒:

Warning: 数组最后一个元素的 $value 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁。

<?php
$arr = [1, 2, 3];

foreach ($arr as $k => &$v) {
    $v = $v * 2;
}
unset($v);
foreach ($arr as $k => $v) {
    echo $v, PHP_EOL;
}

/*
输出:
    2
    4
    6
*/
Copy after login

从这个问题可以看出, 引用很可能会伴随副作用。如果不希望无意识的修改导致数据内容变更, 最好及时unset掉这些引用。

问题二: 如下代码运行结果为何不是 0=>a 1=>b 2=>c

<?php
$arr = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];

foreach ($arr as $k => $v) {
    echo key($arr), "=>", current($arr), PHP_EOL;
}

foreach ($arr as $k => &$v) {
    echo key($arr), "=>", current($arr), PHP_EOL;
}
/*
#php5.6
1=>b 1=>b 1=>b
1=>b 2=>c =>

#php7
0=>a 0=>a 0=>a
0=>a 0=>a 0=>a
*/
Copy after login

按照手册中的说法, key和current分别是获取数据中当前元素的键值。 那为何 key($arr) 一直是0,current($arr) 一直是'a'呢?

先用vld查看编译后的 opcode:

➜  demo /usr/local/Cellar/php/7.2.7/bin/php -dvld.active=1 a.php
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 77) Position 1 = 2, Position 2 = 15
Branch analysis from position: 2
Jump found. (Code = 78) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
Jump found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 15
Jump found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /Users/jianyong/demo/a.php
function name:  (null)
number of ops:  17
compiled vars:  !0 = $arr, !1 = $v, !2 = $k
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   ASSIGN                                                   !0, <array>
   4     1      > FE_RESET_R                                       $4      !0, ->15
         2    > > FE_FETCH_R                                       ~5      $4, !1, ->15
         3    >   ASSIGN                                                   !2, ~5
   5     4        INIT_FCALL                                               &#39;key&#39;
         5        SEND_VAR                                                 !0
         6        DO_ICALL                                         $7
         7        ECHO                                                     $7
         8        ECHO                                                     &#39;%3D%3E&#39;
         9        INIT_FCALL                                               &#39;current&#39;
        10        SEND_VAR                                                 !0
        11        DO_ICALL                                         $8
        12        ECHO                                                     $8
        13        ECHO                                                     &#39;%0A&#39;
        14      > JMP                                                      ->2
        15    >   FE_FREE                                                  $4
   7    16      > RETURN                                                   1

branch: #  0; line:     2-    4; sop:     0; eop:     1; out1:   2; out2:  15
branch: #  2; line:     4-    4; sop:     2; eop:     2; out1:   3; out2:  15
branch: #  3; line:     4-    5; sop:     3; eop:    14; out1:   2
branch: # 15; line:     5-    7; sop:    15; eop:    16; out1:  -2
path #1: 0, 2, 3, 2, 15,
path #2: 0, 2, 15,
path #3: 0, 15,
0=>a
0=>a
0=>a
Copy after login

PHP7新特性之foreach

  • [x] foreach 循环对数组内部指针不再起作用, 在PHP7之前, 当数据通过foreach迭代时, 数组指针会移动。
<?php
$array = [0, 1, 2];
foreach ($array as &$val) {
    var_dump(current($array));
}
Copy after login
版本结果说明
PHP5int(1) int(2) bool(false)数组指针会移动
PHP7int(0) int(0) int(0)数据指针不再移动
  • [x] 按照值进行循环时, 对数组的修改是不会影响循环。

foreach按照值进行循环的时候(by-value), foreach是对该数组的一个拷贝进行操作. 所以在循环过程中修改不影响循环结果

<?php
$arr = [0, 1, 2];
$ref = &$arr;

foreach ($arr as $val) {
    var_dump($val);
    unset($arr[1]);
}
Copy after login
版本结果说明
PHP5int(0) int(2)会将unset的数据跳过
PHP7int(0) int(1) int(2)对数组的改动不影响循环
  • [x] 按照引用进行循环的时候, 对数组的修改会影响循环
<?php
$arr = [0, 1, 2];
$ref = &$arr;

foreach ($arr as &$val) {
    var_dump($val);
    unset($arr[1]);
}
Copy after login
版本 结果
PHP5 int(0) int(2)
PHP7 int(0) int(2)
  • [x] 对简单对象plain(non-Traversable)的循环

在简单对象的循环, 不管是按照值循环还是引用循环, 和按照引用对数组循环的行为是一样的, 不过对位置的管理会更加精确

  • [x] 对迭代对象(Traversable objects)对象行为和之前一致

stackoverflow 上面的解释, Traversable objects is one that implements Iterator or IteratorAggregate interface

如果一个对象实现了 Iterator 或者 IteratorAggregate 接口, 即可称之为迭代对象

推荐学习:《PHP视频教程

The above is the detailed content of An in-depth analysis of foreach in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles