the internal array pointer
The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes: foreach (array_expression as $value) statement
foreach (array_expression as $key => $value) statement
The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one
(so on the next iteration, you'll be looking at the next element).
The second form will additionally assign the current element's key to the $key variable on each iteration. It is possible to customize object iteration.
Note: In PHP 5, when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
foreach 语法结构提供了遍历数组(E iterate over arrays)的简单方式。 foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出(E issue)错误信息。
第一种格式遍历(E loop over )给定的 array_expression 数组。 每次循环中(E on each iteration),当前(E current)单元的值被赋给(E is assiged) $value 并且数组内部的指针(E the inernal array pointer)向前移一步(因此下一次循环中将会得到下一个单元)。 当 foreach 开始执行时,数组内部的指针会自动指向第一个单元。 这意味着不需要在 foreach 循环之前调用 reset()。
<?php
$arr = array(1,2,3,4);
var_dump($value);//null
foreach($arr as &$value){
$value = $value*2;
}
var_dump($value);//
var_dump($arr);// 2 4 6 8 $arrb = array(1,2,3,4);
var_dump($valueb);//null
foreach($arrb as $valueb){
$valueb = $valueb*2;
}
var_dump($valueb);//
var_dump($arrb);//1 2 3 4
the internal array pointer的更多相关文章
- php 设计模式(转)
PhpDesignPatterns [PHP 中的设计模式] 一. Introduction[介绍] 设计模式:提供了一种广泛的可重用的方式来解决我们日常编程中常常遇见的问题.设计模式并不一定就是一个 ...
- PHP之数组函数
php数组中的预定义变量 预定义常量 CASE_LOWER (integer) CASE_LOWER 用在array_change_key_case()中将数组的键名转换为小写字母.这也是array_ ...
- PHP 中最全的设计模式(23种)
https://my.oschina.net/botkenni/blog/1603660 PHP 中最全的设计模式(23种) 原 botkenni 发布于 01/07 21:22 字数 8726 阅读 ...
- php 23种设计模式 - 迭代器模式
迭代器模式 迭代器模式 (Iterator),又叫做游标(Cursor)模式.提供一种方法访问一个容器(Container)对象中各个元素,而又不需暴露该对象的内部细节. 当你需要访问一个聚合对象,而 ...
- c/c++指针总结[pointer summary]
[本文链接] http://www.cnblogs.com/hellogiser/p/pointer-summary.html 1.指针注意事项 (1). 指针类型字符串不容许修改 char *str ...
- Array,ArrayList 和 List<T>的选择和性能比较.
Array Class Provides methods for creating, manipulating, searching, and sorting arrays, thereby serv ...
- Cocos2d-x之Array
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Array是一个列表类容器,是一种线性序列结构:列表容器中的元素是有序的,可以通过下标来访问,就和数组一样.其中Vector也是一种列表容 ...
- PHP7函数大全(4553个函数)
转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...
- 【夯实PHP基础】PHP标准库 SPL
PHP SPL笔记 这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 由于这是供 ...
随机推荐
- python3.4连接和读取oracle数据表
想用python连接Oracle并查询数据表,就写了个Demo.参考了以下网址. Python学习之 cx_Oracle学习记录 一 http://my.oschina.net/bxxfighting ...
- 《转载》脚本实现从客户端服务端HTTP请求快速分析
本文转载自https://www.imooc.com/article/14107 首先我想介绍下,分享这个脚本的用处: 当客户告知我们,一个页面http://www.xxx.com 有问题时,作为PE ...
- PHP关于按位取反结果的推导过程
哎呀几年过去,都快把大学学的计算机导论的知识给忘完了,现在来回顾一下按位去反的流程: <?php /** 首先来补充一下基础知识: php中有4个位运算,分别是&与 |或 ^异或 ~取反 ...
- 怎么安装WinXP和Win7双系统
本文介绍一下如何安装Windows XP和Windows 7双系统以及启动菜单问题的常用修复方法,推荐安装win7和xp双系统时最好采用从低版本到高版本的顺序进行,一般是XP下安装win7,另本文已假 ...
- List的五种去重方式
//set集合去重,不改变原有的顺序 public static void pastLeep1(List<String> list){ System.out.println("l ...
- 如何使用Countifs函数动态统计
我们以前就是一个函数一个功能,一个函数一个区域,这次我们使用动态函数.我们先写好条件范围,因为我们要引用. 同样的我们写下函数出来,我们先把Countifs写出来.选择完区域之后如下图. 条件 ...
- javascript取querystring,存储为hash
function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location ...
- H.264 White Paper学习笔记(二)帧内预测
为什么要有帧内预测?因为一般来说,对于一幅图像,相邻的两个像素的亮度和色度值之间经常是比较接近的,也就是颜色是逐渐变化的,不会一下子突变成完全不一样的颜色.而进行视频编码,目的就是利用这个相关性,来进 ...
- 最小生成树(prime算法 & kruskal算法)和 最短路径算法(floyd算法 & dijkstra算法)
一.主要内容: 介绍图论中两大经典问题:最小生成树问题以及最短路径问题,以及给出解决每个问题的两种不同算法. 其中最小生成树问题可参考以下题目: 题目1012:畅通工程 http://ac.jobdu ...
- 攻防对抗中常用的windows命令(渗透测试和应急响应)
一.渗透测试 1.信息收集类 #查看系统信息 >systeminfo #查看用户信息 >net user >net user xxx #查看网络信息 >ipconfig /al ...