以下为学习孔祥盛主编的《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_数组指针函数的更多相关文章

  1. [C++基础]那些容易被混淆的概念:函数/数组指针-指针函数/数组,类/函数模板-模板类/函数

    函数指针-指针函数 函数指针的重点是指针.表示的是一个指针,它指向的是一个函数.eg: int (*pf)(); 指针函数的重点是函数.表示的是一个函数,它的返回值是指针.eg: int* fun() ...

  2. CPP-基础:函数指针,指针函数,指针数组

    函数指针 函数指针是指向函数的指针变量. 因而“函数指针”本身首先应是指针变量,只不过该指针变量指向函数.这正如用指针变量可指向整型变量.字符型.数组一样,这里是指向函数.如前所述,C在编译时,每一个 ...

  3. c语言指针函数与函数指针

    例一:指针函数 指针函数是指带指针的函数,即本质是一个函数.函数返回类型是某一类型的指针 类型标识符    *函数名(参数表) int *f(x,y); 首先它是一个函数,只不过这个函数的返回值是一个 ...

  4. C++中的指针,指针函数和函数指针

    指针是C或C++中的一大难题,因此弄懂指针对C和C++的学习有很大的帮助,最近一直在研究指针,因此写一篇随笔把心得记录一下. 简单来说指针也是一种变量,只不过指针变量所存储的不是我们直观上看到的,而是 ...

  5. C/C++指针函数和函数指针

    一.指针函数 当一个函数声明其返回值为一个指针时,实际上就是返回一个地址给调用函数,以用于需要指针或地址的表达式中. 格式: 类型说明符 * 函数名(参数) 当然了,由于返回的是一个地址,所以类型说明 ...

  6. 【编程开发】 C与C++中的关于函数指针的强制类型转换与指针函数的关系

    [编程开发] C与C++中的关于函数指针的强制类型转换与指针函数的关系 标签: [编程开发] [VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 以 ...

  7. 指针函数(Pointer Function)和函数指针(Pointer to Function或Function Pointer)

    一.指针函数 1.解释:指针函数很好理解:简单来说,就是一个返回指针的函数,本质是一个函数.如: int fun(int x,int y);    //这是一个普通函数的声明,返回值是一个int类型, ...

  8. 5_PHP数组_3_数组处理函数及其应用_5_数组遍历语言结构

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组遍历语言结构 1. foreach ( array as $value ) 程序: <?php $int ...

  9. php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容

    extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容 它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具

随机推荐

  1. 刷题记录:[De1ctf] shell shell shell

    目录 刷题记录:[De1ctf] shell shell shell 一.知识点 1.源码泄露 2.正则表达式不完善导致sql注入 3.soapclient反序列化->ssrf 4.扫描内网 5 ...

  2. cas 3.5.3服务器搭建+spring boot集成+shiro模拟登录(不修改现有shiro认证架构)

    因为现有系统外部接入需要,需要支持三方单点登录.由于系统本身已经是微服务架构,由多个业务独立的子系统组成,所以有自己的用户认证微服务(不是cas,我们基础设施已经够多了,现在能不增加就不增加).但是因 ...

  3. checkbox与label内的文字垂直居中的解决方案

    <label style="float:left;margin-top:5px;margin-left:10px;cursor:pointer"><input t ...

  4. MacOS Laravel 安装教程

    一.到官网选择 Laravel 版本 根据个人的喜好选择安装的版本,我选择的是 5.8 https://laravel.com/docs/5.8/installation 以下是 Laravel 5. ...

  5. 【LeetCode算法-38】Count and Say

    LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...

  6. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  7. 基于thrift的node.js rpc服务

    1.在node.js 服务下创建node_modules文件,npm install  thrift 下载thrift到该文件下. 2.编写idl文件.user.thrift 内容如下: struct ...

  8. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. [LeetCode] 482. License Key Formatting 注册码格式化

    You are given a license key represented as a string S which consists only alphanumeric character and ...

  10. mybatis对实体的引用必须以 ';' 分隔符结尾

    今天在使用 generate 时(问题起源),由于扫描了mysql所有库下的user表,因此添加参数 nullCatalogMeansCurrent=true 添加改参数解决的原因 查看 但是添加后出 ...