以下为学习孔祥盛主编的《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. Maven中使用<version>LATEST</version>自动依赖最新版本引发的问题

    今天在打包项目的过程中出现了编译问题,奇怪的是这个项目已经好久没有修改过了,报错如下. 找不到符号 [ERROR] 符号: 方法 intent(java.lang.String) [ERROR] 位置 ...

  2. remaining connection slots are reserved for non-replication superuser connections

    使用客户端工具连接pg,连接失败,报错以下错误: FATAL: remaining connection slots are reserved for non-replication superuse ...

  3. ERROR: relation "pg_buffercache" does not exist

    创建pg_buffercache后,查询时报错: postgres=# create extension pg_buffercache; postgres=# select * from pg_buf ...

  4. Java基础 try...catch 处理ArithmeticException 除以零的异常

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

  5. vue---使用Class

    在用vue-cli开发项目的时候,很多时候会用到类.具体的使用方法: config.js(使用类,还可以定义构造函数) class config { /** * 构造函数 * @param {stri ...

  6. Oracle系列十 创建和管理表

    常见的数据库对象 Oracle 数据库中的表 用户定义的表: 用户自己创建并维护的一组表 包含了用户所需的信息 如:SELECT * FROM user_tables;查看用户创建的表 数据字典: 由 ...

  7. Oracle系列三 过滤和排序

    WHERE子句 使用WHERE 子句,将不满足条件的行过滤掉. 示例: SELECT employee_id, last_name, job_id, department_id FROM employ ...

  8. django -xadmin 详解 功能实现及orm 的复习

    django 在xadmin中自定义内容的变量及优化汇总 一: 首先下载xadmin pip install git+git://github.com/sshwsfc/xadmin.git@djang ...

  9. 2018 python获取动态User-Agent

    from fake_useragent import UserAgent ua = UserAgent() headers = {'User-Agent': ua.random} print(ua.r ...

  10. OpenStack Neutron单网卡桥接模式访问外网

    环境配置: * Exsi一台 * Exsi创建的单网卡虚拟机一台 * Ubuntu 14LTS 64位操作系统 * OpenStack Liberty版本 * 使用Neutron网络而非Nova网络 ...