List and Each in PHP

php中文网
发布: 2016-06-23 14:34:25
原创
940人浏览过

list>>>>>>>>>>>>>>>>>>>>>>>>
    sometimes it is very convienent way to do the continual assign,
ruby:

1 a,b,c,d = 1, 2, 3, 4;
登录后复制

php:

1 list($width, $height, $type, $attr) = getImageSize($img);
登录后复制

    list() only works on base of a number indexed array, we can use empty variable to

jump over the unwished value, such as:

1 $info = array('coffee', 'brown', 'caffeine');2 list($drink, , $power) = $info; 
登录后复制

    Extra elements are assigned NULL in list;

立即学习PHP免费学习笔记(深入)”;

    Because list need an numbered array to gain values, when there is an hash array, you

need to do some work to get right numbered array, such as array_values or array_keys;

    list() construct can be used within other list() constructs, such as

1 $matrix = array(array(1,2), array(3,4));2 list(list($a,$b), list($c, $d)) = $matrix;
登录后复制

    List also can be used to swap values between variables without using an intermediary,

such as:

1 list($big, $small) = array($small, $big)
登录后复制

    this can be done no mater how many variables there are. In ruby we do it just on this

simply way

1 a, b = b, a;
登录后复制

Each>>>>>>>>>>>>>>>>>>>>>>>>
    Each() iterate an array key=>value, return an 4 sized array, using 0 and key to refer

original key, using 1 and value to refer original value.

    You have to use reset before you begin iterator an array or after you finish iterate in

order to make the array pointer reset to the 0th element in array.

    Usually each and list are combined to iterate an array in turns untill there are no

elements anymore. such as:

1 $fruit = array('a'=>'apple', 'b'=>'banana', 'c'=>'cranberry');2 reset($fruit);3 while(list($key, $val) = each($fruit))4 {5     echo "$key => $value\n";6 }
登录后复制

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号