PHP 数组 (前篇) 遍历

WBOY
Release: 2016-06-23 13:44:36
Original
1185 people have browsed it

<?php /** * Created by PhpStorm. * User: W.J.Chang * Date: 2014/11/24 * Time: 16:28 *//* * php 数组相当灵活 * 1, 索引数组:和其他语言一样,用数字下标进行索引,但是下标可以不连续 *    $demo = array('apple','orange','banana') <==> demo = array(0=>'apple',1=>'orange',2=>'banana'); * 2, 关联数组:key-value形式 * 3, 多维数组:数组的数组 $demo = array(array(),array()); *//* * 数组遍历 * 1, for *    这种方式不是首选 *    数组必须是下标连续的索引数组 * * 2, foreach *    foreach($arr as $item) { ... } *    foreach($arr as $key=>$val) { ... } * * 3, while() list() each() 组合 *    each(): 需要一个数组作为参数,返回array(0,1,$key,$val),是一个游标操作,执行到最后会返回false *    list(): a, list() = array() 需要将数组赋给list() *            b, 数组中的元素个数要和list()函数中的参数个数相同 *            c, 数组中的每个元素会赋值给list()中的每个参数 *            d, list() 只能接受索引数组 *            e, 按索引的下标顺序遍历 * *    数组内部指针控制函数 * *    prev($arr)      : 上一个指针 *    next($arr)      : 下一个指针 *    reset($arr)     : 第一个 *    end($arr)       : 最后一个 * *    current($arr)   : 当前key *    key($arr)       : 当前value * */// forecho "\n for ... \n";$demo = array('apple','orange','banana');for($i=0;$i<count echo forecho for ... array>'apple',1=>'orange',2=>'banana');for($i=0;$i<count echo foreachecho foreach ... as> $val) {    echo ' '.$key.'=>'.$val.' ';}echo "\n".'----------------------------------------';$user = array('id'=>1,'name'=>'jack','age'=>10,'sex'=>'male');// whileecho "\n while ... \n";while($arr=each($user)) {    // "$arr[0]-->$arr[1]";    echo  $arr['key']."-->".$arr['value']."  ";}echo "\n-------------------------------------------";while(list($key,$value)=each($user)) {    echo ''.$key.'----->'.$value."  ";}echo "\n-------------------------------------------\n";reset($user);while(list($key,$value)=each($user)) {    echo ''.$key.'----->'.$value."  ";}echo "\n-------------------------------------------\n";while(list($key,$value)=each($user)) {    echo ''.$key.'----->'.$value."  ";}reset($user);next($user);next($user);echo "\n-------------------------------------------\n";while(list($key,$value)=each($user)) {    echo ''.$key.'----->'.$value."  ";}reset($user);echo "\n";echo current($user).'---------->'.key($user)."\n";end($user);echo current($user).'---------->'.key($user)."\n";prev($user);echo current($user).'---------->'.key($user)."\n";/* * result * for ...apple orange banana for ...apple orange banana foreach ...apple orange banana foreach ... 0=>apple  1=>orange  2=>banana---------------------------------------- while ...id-->1  name-->jack  age-->10  sex-->male--------------------------------------------------------------------------------------id----->1  name----->jack  age----->10  sex----->male--------------------------------------------------------------------------------------age----->10  sex----->male1---------->idmale---------->sex10---------->age */</count></count>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template