PHP next
1.函数的作用:返回数组当前元素位置的下一个元素
2.函数的参数:
@param array &$array
3.
例子一:数组拷贝时,内部指针的位置也一起拷贝
<?php
$arr1 = ['last','next'];
next($arr1);
$arr2 = $arr1;
echo "Pointer of arr1 is " .key($arr1) .". The value is '" . current($arr1) ."'\n";
echo "Pointer of arr2 is " .key($arr2) .". The value is '" . current($arr1) ."'\n";

例子二: foreach 之后数组内部指针的位置不重置
<?php
$arr1 = ['last','next'];
foreach($arr1 as $key => $value){
echo "Number $key's value : $value\n";
}
$str = is_null(key($arr1));
echo "The current key of the array of arr1 is " . ($str ? 'null' : $str) ;

例子三:
<?php
$arr1 = ['last','next'];
next($arr1);
$arr2 = array_values($arr1); echo "The pointer's position of the array of arr1 is " .key($arr1) . "\n";
echo "The pointer's position of the array of arr1 is " .key($arr2) . "\n";

例子四:接下来是比较奇异的两个地方,传数组参数给函数,看看指针的位置的情况:
1)指针重置的情况:
<?php
function testPointerPosition($array){
echo "The current element of array in function is '" .current($array)."' and current key is " .key($array)."\n";
} $arr1 = ['last','next'];
next($arr1);
next($arr1);
testPointerPosition($arr1);

2)指针未重置的情况:
<?php
function testPointerPosition($array){
echo "The current element of array in function is '" .current($array)."' and current key is " .key($array)."\n";
} $arr1 = ['last','next'];
next($arr1);
testPointerPosition($arr1);

例子五:有的时候使用next()函数之后,你想判断该元素是不是存在,结果你这么用:
<?php
$arr = [1,false];
next($arr);
if(current($arr)){
echo "The element exist!\n";
}else{
echo "The element doesn't exist!\n";
}

刚好有个 false 元素,就有了错误的输出。所以应该这么用:
<?php
$arr = [1,false];
next($arr);
if(key($arr) === false){
echo "Current element doesn't exist!\n";
}else{
echo "Current element exist!\n";
}

记住用 “===” 符号。数组不会有键值为false的类型的,即使你初始化的时候,用false作键值,内部也会将其解释为 0,并覆盖先前的键值为 0 的元素。
随机推荐
- java使用FileSystem上传文件到hadoop分布式文件系统配置
Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://sparkclust ...
- Hessian 接口使用示例总结(转载)
一.使用hessian接口准备 首先,hessian接口的使用,必须要准备hessian接口的jar包,本文使用的jar包如下:hessian-4.0.7.jar; Hessian接口的使用一般是在两 ...
- python time.striptime模块用法
python time模块中strptime用法 time.strptime(string[, format]) 其中string参数即为要深入的日期字符串.format为格式化的日期字符串. %Y ...
- ES6入门八:Promise异步编程与模拟实现源码
Promise的基本使用入门: ——实例化promise对象与注册回调 ——宏任务与微任务的执行顺序 ——then方法的链式调用与抛出错误(throw new Error) ——链式调用的返回值与传值 ...
- Widget 基础
一切皆Widget Widget 渲染过程 Flutter把视图数据的组织和渲染抽象为三部分,即 Widget.Element 和 RenderObject. Widget Widget 是空间实现的 ...
- Spark学习之RDDs介绍
什么是RDDS? RDDS即Resilient distributed datasets(弹性分布式数据集). Spark中,所有计算都是通过RDDs的创建,转换,操作完成的. 一个RDD是一个不可改 ...
- Hadoop核心组件之HDFS
HDFS:分布式文件系统 一句话总结 一个文件先被拆分为多个Block块(会有Block-ID:方便读取数据),以及每个Block是有几个副本的形式存储 1个文件会被拆分成多个Block blocks ...
- Android 禁止Edittext弹出系统软键盘 的几种方法
第一种方法:在XML文件下添加: android:focusable="true" android:focusableInTouchMode="true" 第二 ...
- 异步Promise及Async/Await最完整入门攻略
一.为什么有Async/Await? 我们都知道已经有了Promise的解决方案了,为什么还要ES7提出新的Async/Await标准呢? 答案其实也显而易见:Promise虽然跳出了异步嵌套的怪圈, ...
- VR中的“寻路(wayfinding)”
虚拟现实(VR)中很重要的一个问题就是Locomotion(用户在VR中的移动).这个Locomotion分为两种,一种是点对点的,如传送门的方式,一种是包含了可以操控的中间过程的,这种被称为“导航( ...