PHP 遍历数组语句总结(foreach,for,list,each)
发布:smiling 来源: PHP粉丝网 添加日期:2014-02-23 11:47:22 浏览: 评论:0
在php中遍历循环读出数组的方法有几种,foreach,for,list,each,while都是可以的,只是那种更适合用来遍历数组了。
foreach来访问,遍历的顺序是固定的么? 以什么顺序遍历呢?
比如:代码如下:
- <?php
- $colors= array('red','blue','green','yellow');
- foreach ($colors as $color){
- //add your codes
- }
- ?>
例2,代码如下:
- $capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
- foreach($capitals as $key=> $val){
- //add your codes
- }
while()
while() 通常和 list(),each()配合使用,实例代码如下:
- <?php
- $colors = array('red','blue','green','yellow');
- while(list($key,$val) = each($colors)) {
- echo "Other list of $val.<br />";
- }
- ?>
- /*
- 显示结果:
- Other list of red.
- Other list of blue.
- Other list of green.
- Other list of yellow.
- */
3. for(),实例代码如下:
- <?php
- $arr = array ("0" => "zero","1" => "one","2" => "two");
- for ($i = 0;$i < count($arr); $i++) {
- $str = $arr[$i];
- echo "the number is $str.<br />";
- }
- ?>
- /*
- 显示结果:
- the number is zero.
- the number is one.
- the number is two.
- */
以下是函数介绍:
key()
mixed key(array input_array)
key()函数返回input_array中位于当前指针位置的键元素。
实例代码如下:
- <?php
- $capitals = array("Ohio" => "Columbus","Towa" => "Des Moines","Arizona" => "Phoenix");
- echo "<p>Can you name the capitals of these states?</p>";
- while($key = key($capitals)) {
- echo $key."<br />";
- next($capitals);
- //每个key()调用不会推进指针。为此要使用next()函数
- }
- ?>
- /*结果如下
- Can you name the capitals of these states?
- Ohio
- Towa
- Arizona
- */
each() 函数遍历数组
例子1,代码如下:
- <?php
- $people = array("Peter", "Joe", "Glenn", "Cleveland");
- print_r (each($people));
- ?>
- //输出:
- //Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )
例子2,代码如下:
each() 经常和 list() 结合使用来遍历数组,本例与上例类似,不过循环输出了整个数组,代码如下:
- <?php
- $people = array("Peter", "Joe", "Glenn", "Cleveland");
- reset($people);
- while (list($key, $val) = each($people))
- {
- echo "$key => $val<br />";
- }
- ?>
- /*
- 输出:
- 0 => Peter
- 1 => Joe
- 2 => Glenn
- 3 => Cleveland
- */
多维数组的递归遍历,代码如下:
- <?php
- /*
- * -------------------------------------------------
- * Author :
- * Url : www.phpfensi.com* Date : 2011-03-09
- * -------------------------------------------------
- */
- function arr_foreach ($arr)
- {
- if (!is_array ($arr))
- {
- return false;
- }
- foreach ($arr as $key => $val )
- {
- if (is_array ($val))
- {
- arr_foreach ($val);
- }
- else
- {
- echo $val.'<br/>';
- }
- }
- }
- $arr1 = array (1=>array(11,12,13,14=>array(141,142)),2,3,4,5);
- echo '<pre>';
- print_r($arr1);
- echo '<pre>';
- arr_foreach ($arr1);
- ?>
- /*
- 结果
- Array
- (
- [1] => Array
- (
- [0] => 11
- [1] => 12
- [2] => 13
- [14] => Array
- (
- [0] => 141
- [1] => 142
- )
- )
- [2] => 2
- [3] => 3
- [4] => 4
- [5] => 5
- )
- 11
- 12
- 13
- 141
- 142
- 2
- 3
- 4
- 5
- */
Tags: 遍历 数组 语句 foreach for list
相关文章
- ·PHP数组的高级遍历和操作处理方法(2014-01-07)
- ·php中数组遍历循环实现程序(2014-01-16)
- ·php 遍历多维数组方法(2014-01-16)
- ·php学习笔记 数组的声明与遍历(2014-02-10)
- ·PHP学习笔记之数组的定义、遍历(2014-02-10)
- ·php foreach遍历数组用法介绍(2014-02-28)
- ·php一维数组遍历方法(2014-05-12)
- ·php遍历输出数组的方法(2014-05-21)
- ·php foreach 循环遍历数组方法(2014-05-27)
- ·php遍历循环数组实现方法(2014-05-31)
- ·php遍历二维数组 foreach函数(2014-06-04)
- ·php数组的遍历函数与方法例子(2014-06-15)
- ·php中数据排序与遍历函数总结(2014-08-04)
- ·php遍历数组几种方法(2014-08-04)
- ·php遍历数组高级操作详解(2014-08-27)
- ·php 数组遍历性能的比较(2014-09-05)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)