php foreach遍历
foreach($facility_list['data'] as $facility){
//处理语句
}
第一种格式遍历给定的 array_expression_r_r 数组。每次循环中,当前单元的值被赋给 $value 并且数组内部的指针向前移一步(因此下一次循环中将会得到下一个单元)。
第二种格式做同样的事,只除了当前单元的键值也会在每次循环中被赋给变量 $key。
注: 当 foreach 开始执行时,数组内部的指针会自动指向第一个单元。这意味着不需要在 foreach 循环之前调用 reset()。
注: 此外注意 foreach 所操作的是指定数组的一个拷贝,而不是该数组本身。因此即使有 each() 的构造,原数组指针也没有变,数组单元的值也不受影响。
注: foreach 不支持用“@”来禁止错误信息的能力。
假设请求过来的数据格式:
$facility_list变量值为:
array(1) { ["data"]=> array(3) { [0]=> array(4) { ["facility_id"]=> string(1) "1" ["facility_name"]=> string(9) "嘉兴仓" ["schedule_mode"]=> string(29) "manual_schedule_auto_shipping" ["facility_type"]=> string(1) "1" } [1]=> array(4) { ["facility_id"]=> string(1) "2" ["facility_name"]=> string(9) "广州仓" ["schedule_mode"]=> string(29) "manual_schedule_auto_shipping" ["facility_type"]=> string(1) "1" } } }
这里是一个数组array(1),所以我们需要获取里面的数据,需要去这个数组中data指向的值,
$facility_list['data']变量值为:
array(3) { [0]=> array(4) { ["facility_id"]=> string(1) "1" ["facility_name"]=> string(9) "嘉兴仓" ["schedule_mode"]=> string(29) "manual_schedule_auto_shipping" ["facility_type"]=> string(1) "1" } [1]=> array(4) { ["facility_id"]=> string(1) "2" ["facility_name"]=> string(9) "广州仓" ["schedule_mode"]=> string(29) "manual_schedule_auto_shipping" ["facility_type"]=> string(1) "1" } }
通过这个语句获取到如下数组,相当于从外部数组进入了内部数组中,数组中包含数组,对内部数组在对每个值进行遍历,例如:
$facility['facility_id']变量值为:
string(1) "1"
例子:
$facility_list = $this->common->getFacilityList();
$facility_id = $this->getInput('facility_id');
if (isset($facility_id)) {
$cond['facility_id'] = $facility_id;
foreach($facility_list['data'] as $facility){
if($facility['facility_id']==$facility_id){
$cond['facility_type']=$facility['facility_type'];
}
}
} else {
$cond['facility_id'] = $facility_list['data'][0]['facility_id'];
$cond['facility_type'] = $facility_list['data'][0]['facility_type'];
}
php foreach遍历的更多相关文章
- 用<forEach>遍历list集合时,提示我找不到对象的属性
<c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...
- Foreach遍历
前天在项目中遇到一个问题,foreach遍历过程中修改responses中的对象,其中responses的类型:IEnumerable<Order>,代码如下: foreach (Orde ...
- 使用yield关键字让自定义集合实现foreach遍历
一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...
- js中三个对数组操作的函数 indexOf()方法 filter筛选 forEach遍历 map遍历
indexOf()方法 indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. 不使用indexOf时 var arr = ['apple','orange','pea ...
- mybatis map foreach遍历
mybatis map foreach遍历 转至http://www.cnblogs.com/yg_zhang/p/4314602.html mybatis 遍历map实例 map 数据如下 Map& ...
- foreach遍历遇到的一个细节问题
1.Invalid argument supplied for foreach()警告错误解决办法:foreach遍历之前添加is_array()判断
- IEnumerable 接口 实现foreach 遍历 实例
额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答: 该类实现IEnumetable 接口 声明 ...
- 实现Foreach遍历
实现Foreach遍历的集合类,需要实现IEnumerable接口,泛型集合则需要实现IEnumerable<T>接口 using System; using System.Collect ...
- foreach遍历扩展(二)
一.前言 假设存在一个数组,其遍历模式是根据索引进行遍历的:又假设存在一个HashTable,其遍历模式是根据键值进行遍历的:无论哪种集合,如果它们的遍历没有一个共同的接口,那么在客户端进行调用的时候 ...
- c#--foreach遍历的用法与split的用法
一. foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素. 该循环 ...
随机推荐
- 4 个快速的 Python 编译器 for 2018
简评:Python 和其他的解释型语言一样经常被吐槽性能不行,所以开发人员为了提升性能创建了不少编译器,本文则选取其中的四个做了基准测试. Python 其实是一种相当快的语言,但它并不像编译型语言那 ...
- Linux一些常用的基础命令,总结的很好,收藏了
原文地址:https://www.cnblogs.com/yjd_hycf_space/p/7730690.html
- C#获取当前程序集的完整路径
//获取当前程序集的完整路径加上EXE的名称 string binPath = Assembly.GetExecutingAssembly().Location; Console.WriteLine( ...
- Python爬取LOL英雄皮肤
Python爬取LOL英雄皮肤 Python 爬虫 一 实现分析 在官网上找到英雄皮肤的真实链接,查看多个后发现前缀相同,后面对应为英雄的ID和皮肤的ID,皮肤的ID从00开始顺序递增,而英雄ID跟 ...
- [转] Centos 系统swap虚拟内存添加与删除配置
[From]https://blog.csdn.net/lengyue1084/article/details/51405640 [From]https://yuukis.cn/24/ SWAP是Li ...
- supervisor使用小记
最近使用supervisor部署爬虫,百度了很多,磕磕绊绊自己也算是用起来了,以下是整理的使用情况. 第一步: 下载安装supervisor 使用的ubuntu16.04,直接 sudo apt-ge ...
- 手写JDBC - 数据库、驱动信息存储在配置文件
1. 将数据库.驱动信息存储在配置文件 configure.properties url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT& ...
- es6-函数的扩展
/* * 1:函数参数的默认值 * */ !(() => { function f(x, y = 1, z = 1, g = 1) { console.log(x, y, z, g);//1,1 ...
- C#文件上传编码乱码
又遇到文件编码乱码的事情,这回稍微有些头绪,但是还是花了很多时间去解决. 场景:上传csv文件,导入到数据库.上传文件的编码不定,需要转成unicode存储. 问题点:需要解决判断上传文件的编码. 关 ...
- Ubuntu禁用Compiz
环境: Ubuntu 16.04 什么是Compiz: https://zh.wikipedia.org/wiki/Compiz ,简单理解是实现了Ubuntu的3D效果 禁用的好处: 节省内存和CP ...