以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。

数组遍历语言结构

1. foreach ( array as $value )

程序:

 <?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
foreach ( $interests as $value) {
echo $value."<br/>";
}
?>

输出:

music
movie
computer
software

2. foreach( array as $key=>$value )

程序:

 <?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
foreach ( $interests as $key=>$value) {
echo $key."=>".$value."<br/>";
}
?>

输出:

2=>music
5=>movie
1=>computer
6=>software

foreach 语言结构除了可以实现对数组的遍历,还可以实现数组元素的键或值的修改

以下程序1、程序2、程序3,只有程序1、2实现了数组元素值的修改。

程序1:

 <?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
foreach ( $interests as $key=>&$value) { //注意这里的 &$value
$value = "I like ".$value;
}
print_r($interests);
//Array ( [2] => I like music [5] => I like movie [1] => I like computer [6] => I like software )
?>

输出:

Array ( [2] => I like music [5] => I like movie [1] => I like computer [6] => I like software )

程序2:

 <?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
foreach ( $interests as $key=>$value) {
$interests[$key] = "I like ".$value;
}
print_r($interests);
?>

输出:

Array ( [2] => I like music [5] => I like movie [1] => I like computer [6] => I like software )

程序3:

 <?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
foreach ( $interests as $key=>$value) {
$value = "I like ".$value;
}
print_r($interests);
?>

输出:

Array ( [2] => music [5] => movie [1] => computer [6] => software )

foreach 语言结构操作的是数组的一个拷贝,而不是数组本身。在foreach 遍历数组的过程,尽量不要使用数组指针函数操作 “当前指针”(current),否则会事与愿违。

程序:

 <?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
foreach ( $interests as $key=>$value) {
echo "I like ".current($interests)."!<br/>";
echo $value."<br/>";
}
print_r($interests);
?>

输出:

I like music!
music
I like music!
movie
I like music!
computer
I like music!
software
Array ( [2] => music [5] => movie [1] => computer [6] => software )

5_PHP数组_3_数组处理函数及其应用_5_数组遍历语言结构的更多相关文章

  1. 剑指Offer 13. 调整数组顺序使奇数位于偶数前面 (数组)

    题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 题目地址 https ...

  2. (转)PHP的语言结构和函数的区别

    相信大家经常看到对比一些PHP应用中,说用isset() 替换 strlen(),isset比strlen执行速度快等. 例子: if ( isset($user) ) { //do some thi ...

  3. PHP 语言结构(Language constructs)和函数的区别

    相信大家经常看到对比一些PHP应用中,说用isset() 替换 strlen(),isset比strlen执行速度快等. 例子: if ( isset($username[5]) ) { // The ...

  4. PHP的语言结构和函数的区别

    相信大家经常看到对比一些PHP应用中,说用isset() 替换 strlen(),isset比strlen执行速度快等. 例子: if ( isset($user) ) { //do some thi ...

  5. PHP:函数和语言结构(转)

    转自:https://www.cnblogs.com/fanqiechaodan/articles/5222366.html 什么是语言结构呢?它和函数有什么不同吗? 1.  什么是语言结构和函数 语 ...

  6. 5_PHP数组_3_数组处理函数及其应用_3_数组指针函数

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组指针函数 1. key() 函数 程序: <?php $interests[2] = "mus ...

  7. 5_PHP数组_3_数组处理函数及其应用_4_数组和变量间的转换函数

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组和变量间的转换函数 1. list() 语言结构 程序: <?php $info = array('co ...

  8. PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?

    如题:如何判断一个数组是一维数组或者是二维数组?用什么函数? 判断数量即可 <?php if (count($array) == count($array, 1)) { echo '是一维数组' ...

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

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

随机推荐

  1. 【2019.11.18】SDN阅读作业

    为什么需要SDN?SDN特点? 随着网络的快速发展,传统互联网出现了如传统网络配置复杂度高等诸多问题,这些问题说明网络架构需要革新,可编程网络的相关研究为 SDN 的产生提供了可参考的理论依据 SDN ...

  2. plsql 如何导入excel数据?

      oracle 导入excel数据? 通过plsql实现 1.准备工作 Excel中的字段名称,必须和表结构字段一 一对应 下面以tdoctor_apply表为例,进行演示 表结构 Excel表数据 ...

  3. MacBook Air在macOS Mojave和macOS Seirra系统下使用Loopback在OBS Studio推流时输出系统软件声音

    转载请标注原地址:https://www.cnblogs.com/lixiaojing/p/11440533.html 运行环境: Loopback破解版获取: https://pan.baidu.c ...

  4. mocha单元测试简易教程

    mocha单元测试简易教程 写在前面 其实mocha单元测试的教程网上有很多,也都很简单易懂,但是每个人对同一份的教程也会产生不同的理解,像我这种大概就是走遍了所有弯路才到达终点的人,想通过给大家分享 ...

  5. Java基础 三目运算符 用if-else对其进行解释

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  6. Bladex使用代码生成器操作步骤

    一.从私服上下载BladeX和Saber 二.运行BladeX所有服务 三.运行Saber 四.数据库创建自己需要使用的表(建议表名和字段名为:bldex_xxxx,xxx_xxxx,不要使用驼峰命名 ...

  7. [转]彻底解决deepin linux的无线网络问题

    链接地址:https://bbs.deepin.org/forum.php?mod=viewthread&tid=153154

  8. 【c++基础】C与C++接口相互调用

    前言 编译程序的时候出现错误,入口程序如果是cpp文件可以编译成功,如果是c程序则出错.一般这个问题是c与c++之间接口相互调用出现的问题. 出现的错误是undefined reference to ...

  9. Oracle-关于Oracle.ManagedDataAccess

    今天调用webservice的时候,运行程序后开始报错以下的错误信息 “/”应用程序中的服务器错误. 未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项.试图加载格式不正确的 ...

  10. Graphviz(01) notepad++ Run xxx.gv

    1.新建文件dot.bat 放到 d:\,内容如下: set gvname=%% @echo '"d:\dot.bat" "$(FULL_CURRENT_PATH)&qu ...