5_PHP数组_3_数组处理函数及其应用_3_数组指针函数
以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。
数组指针函数

1. key() 函数
程序:
<?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
var_dump(key($interests)); //int 2 //返回数组 arr 中"当前指针"所指元素的键名。
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:int 2
2. current() 函数
程序:
<?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
var_dump(current($interests)); //string 'music' (length=5) //返回数组 arr 中“当前指针”所指元素的“值”
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:6:string 'music' (length=5)
3. next() 函数
程序:
<?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
$second = next($interests);
$third = next($interests);
var_dump(key($interests)); //int 1
echo "<br/>";
var_dump(current($interests)); //string 'computer' (length=8)
echo "<br/>";
var_dump($second); //string 'movie' (length=5)
echo "<br/>";
var_dump($third); //string 'computer' (length=8)
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:int 1 D:\wampServer\www\Apache服务器主目录\practise\例程.php:10:string 'computer' (length=8) D:\wampServer\www\Apache服务器主目录\practise\例程.php:12:string 'movie' (length=5) D:\wampServer\www\Apache服务器主目录\practise\例程.php:14:string 'computer' (length=8)
4. end() 函数
程序:
<?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
$end = end($interests);
var_dump(key($interests)); //int 6
echo "<br/>";
var_dump(current($interests)); //string 'software' (length=8)
echo "<br/>";
var_dump($end); //string 'software' (length=8)
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:7:int 6 D:\wampServer\www\Apache服务器主目录\practise\例程.php:9:string 'software' (length=8) D:\wampServer\www\Apache服务器主目录\practise\例程.php:11:string 'software' (length=8)
5. prev() 函数
程序:
<?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
$end = end($interests);
$prev = prev($interests);
var_dump(key($interests)); //int 1
echo "<br/>";
var_dump(current($interests)); //string 'computer' (length=8)
echo "<br/>";
var_dump($end); //string 'software' (length=8)
echo "<br/>";
var_dump($prev); //string 'computer' (length=8)
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:int 1 D:\wampServer\www\Apache服务器主目录\practise\例程.php:10:string 'computer' (length=8) D:\wampServer\www\Apache服务器主目录\practise\例程.php:12:string 'software' (length=8) D:\wampServer\www\Apache服务器主目录\practise\例程.php:14:string 'computer' (length=8)
6. reset() 函数
程序:
<?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
$end = end($interests);
$first = reset($interests);
var_dump(key($interests)); //int 2
echo "<br/>";
var_dump(current($interests)); //string 'music' (length=5)
echo "<br/>";
var_dump($end); //string 'software' (length=8)
echo "<br/>";
var_dump($first); //string 'music' (length=5)
?>
输出:
D:\wampServer\www\Apache服务器主目录\practise\例程.php:8:int 2 D:\wampServer\www\Apache服务器主目录\practise\例程.php:10:string 'music' (length=5) D:\wampServer\www\Apache服务器主目录\practise\例程.php:12:string 'software' (length=8) D:\wampServer\www\Apache服务器主目录\practise\例程.php:14:string 'music' (length=5)
7. each() 函数
程序:
<?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
$each = each($interests);
print_r( $each ); //Array ( [1] => music [value] => music [0] => 2 [key] => 2 )
echo "<br/>";
echo current($interests); //movie
?>
输出:

说明:
PHP 7.2 废弃了each()方法。
数组的遍历
使用list()语言结构、each()函数和循环语句可以实现
程序:
<?php
$fruits = array( 'orange', 'apple', 'banana');
$colors = array( 'orange', 'red', 'yellow' );
$temp = array_combine( $fruits,$colors );
do{
$key = key($temp);
$value = current($temp);
echo $key." => ".$value."<br/>";
}while( next($temp) );
?>
输出:
orange => orange
apple => red
banana => yellow
5_PHP数组_3_数组处理函数及其应用_3_数组指针函数的更多相关文章
- [C++基础]那些容易被混淆的概念:函数/数组指针-指针函数/数组,类/函数模板-模板类/函数
函数指针-指针函数 函数指针的重点是指针.表示的是一个指针,它指向的是一个函数.eg: int (*pf)(); 指针函数的重点是函数.表示的是一个函数,它的返回值是指针.eg: int* fun() ...
- CPP-基础:函数指针,指针函数,指针数组
函数指针 函数指针是指向函数的指针变量. 因而“函数指针”本身首先应是指针变量,只不过该指针变量指向函数.这正如用指针变量可指向整型变量.字符型.数组一样,这里是指向函数.如前所述,C在编译时,每一个 ...
- c语言指针函数与函数指针
例一:指针函数 指针函数是指带指针的函数,即本质是一个函数.函数返回类型是某一类型的指针 类型标识符 *函数名(参数表) int *f(x,y); 首先它是一个函数,只不过这个函数的返回值是一个 ...
- C++中的指针,指针函数和函数指针
指针是C或C++中的一大难题,因此弄懂指针对C和C++的学习有很大的帮助,最近一直在研究指针,因此写一篇随笔把心得记录一下. 简单来说指针也是一种变量,只不过指针变量所存储的不是我们直观上看到的,而是 ...
- C/C++指针函数和函数指针
一.指针函数 当一个函数声明其返回值为一个指针时,实际上就是返回一个地址给调用函数,以用于需要指针或地址的表达式中. 格式: 类型说明符 * 函数名(参数) 当然了,由于返回的是一个地址,所以类型说明 ...
- 【编程开发】 C与C++中的关于函数指针的强制类型转换与指针函数的关系
[编程开发] C与C++中的关于函数指针的强制类型转换与指针函数的关系 标签: [编程开发] [VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 以 ...
- 指针函数(Pointer Function)和函数指针(Pointer to Function或Function Pointer)
一.指针函数 1.解释:指针函数很好理解:简单来说,就是一个返回指针的函数,本质是一个函数.如: int fun(int x,int y); //这是一个普通函数的声明,返回值是一个int类型, ...
- 5_PHP数组_3_数组处理函数及其应用_5_数组遍历语言结构
以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组遍历语言结构 1. foreach ( array as $value ) 程序: <?php $int ...
- php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容
extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容 它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具
随机推荐
- 导入eclipse有Unbound classpath variable: 'M2_REPO报错的解决方法
Eclipse maven of the project reported in Unbound classpath variable: 'M2_REPO /**/***/***. jar' But ...
- js - 总结一下条件语句优化
[笔记] // 简单的语句用三目运算符也可以的(除了需要return的) 1 == 1 ? console.log('执行了...1') : console.log(); 1 == 2 ? conso ...
- gdal 根据条件选择数据
- nodejs爬虫如何设置动态ip以及userAgent
nodejs爬虫如何设置动态ip以及userAgent 转https://blog.csdn.net/u014374031/article/details/78833765 前言 在写nodejs爬虫 ...
- Python个人笔记
目录 前言 查看Python以及第三方包安装位置 Python打包成exe 安装pyinstaller打包工具 打包 Python操作json 储存json 读取json 中文问题 倒计时关闭程序 P ...
- Mudos扩展efunc,packages方式
Mudos扩展efunc,packages方式 首先packages目录建好C文件 我们这里测试了一个mongodb的 mongodb_spec.c mongodb.h mongodb.c 这里具体的 ...
- Docker中安装mysql
1.docker 中下载 mysql docker pull mysql 2.启动 docker run -itd --name mysql -p 3306:3306 -e MYSQL_ROOT_PA ...
- LeetCode 566. 重塑矩阵(Reshape the Matrix)
566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...
- LeetCode 162. 寻找峰值(Find Peak Element) 29
162. 寻找峰值 162. Find Peak Element 题目描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元 ...
- [转帖]JAVA BIO与NIO、AIO的区别(这个容易理解)
JAVA BIO与NIO.AIO的区别(这个容易理解) https://blog.csdn.net/ty497122758/article/details/78979302 2018-01-05 11 ...