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 的元素。
随机推荐
- .Net基础篇_学习笔记_第四天_加加减减
using System.Linq; using System.Text; using System.Threading.Tasks; namespace 第四天_加加减减 { class Progr ...
- eclipse搭建springmvc
https://www.cnblogs.com/qixing/p/qixing.html
- [Advanced Python] 16 - Google style guide for programming
Ref: Python 风格指南 - 内容目录 这里主要记录一下值得注意的地方. Python语言规范 Lint:vsCode自带 导入:完整路径 异常 Ref: [Python] 07 - Stat ...
- java取json 的方法
public static void main(String[] args) { String jsonStr = "[{\"varieties_type\":\&quo ...
- PMP全真模拟题真题試題含答案解析 2019年下半年PMP考試适用 PMP中文文对照试题 【香港台灣地區PMP考試也可用】
PMP全真模拟题真题试题 含答案解析 2019年下半年PMP考试适用 PMP中文文对照试题 [香港台灣地區PMP考試也可用]PMP全真模擬題真題試題 含答案解析 2019年下半年PMP考試适用 PMP ...
- python打开文件查询字符串时报UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 19: illegal multibyte sequence错误
当这样打开时报错了 lines = open(path).readlines() open(path).close() for line in lines: idx1 = line.find('检测到 ...
- 23种设计模式之适配器模式(Adapter Pattern)
适配 即在不改变原有实现的基础上,将原先不兼容的接口转换为兼容的接口.例如:二转换为三箱插头,将高电压转换为低电压等. 动机(Motivate): 在软件系统中,由于应用环境的变化,常常需要将“ ...
- jquery 动态控制显隐
1.第1种方法 ,给元素设置style属性 $("#hidediv").css("display", "block"); 2.第2种方法 , ...
- 高效解决「SQLite」数据库并发访问安全问题,只这一篇就够了
Concurrent database access 本文译自:https://dmytrodanylyk.com/articles/concurrent-database/ 对于 Android D ...
- 针对永久不过期的key 批量设置过期时间
问题需求: redis内存暴增,后来发现有很多设置永久不过期. 解决:查找出来之后针对前缀批量设置过期时间 (过期时间与开发沟通 保证服务不受影响) 来源于网上杨一的代码 正好解决了我遇到的问题 在这 ...