zoukankan      html  css  js  c++  java
  • 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()。

     1 <?php
     2 $arr = array(1,2,3,4);
     3 var_dump($value);//null
     4 foreach($arr as &$value){
     5     $value = $value*2;
     6 }
     7 var_dump($value);//8
     8 var_dump($arr);// 2 4 6 8
     9 
    10 $arrb = array(1,2,3,4);
    11 var_dump($valueb);//null
    12 foreach($arrb as $valueb){
    13     $valueb = $valueb*2;
    14 }
    15 var_dump($valueb);//8
    16 var_dump($arrb);//1 2 3 4 
  • 相关阅读:
    [Taro] 解决 使用 Taro UI 小程序下 Iconfont 图标 不显示问题
    [Taro] Taro 环境安装 (一)
    [RN] react-native FlatList 实现列表选中的最佳方式(刷新指定Item)
    [RN] React Native FlatList 选中后 状态没有立即发生改变,而在下一次生效的问题
    [RN] React Native 使用 Redux 比较详细和深刻的教程
    [Taro] taro 缓存
    个人总结第十五周
    个人总结第十四周
    个人总结第十三周
    个人总结第十二周
  • 原文地址:https://www.cnblogs.com/rsapaper/p/5859872.html
Copyright © 2011-2022 走看看