rule 发表于 2017-4-5 12:38:03

PHP循环结合数组和list的使用

  foreach的使用:
1.用法1

http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif  $arr=array("张三","李四","王五","马六");
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif  foreach($arr as$value)
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif  echo $value.'<br/>';
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  
2.用法2
<?php
$arr=array('chinese'=>130,'math'=>140,'english'=>135,'computer'=>130);
foreach($arr as $key=>$value)
echo $key.':'.$value.'<br/>';
?>

  
3.按拷贝还是引用
默认按拷贝,和函数很相似:

<?php
    $arr=array(1,2,3,4);
    foreach($arr as $value)
    $value*=2;//其实改变的是副本对原数组没有影响
    print_r($arr);//Array ( => 1 => 2 => 3 => 4 )
?>

  
但是可以改成引用方式,通过&符号:

http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif    $arr=array(1,2,3,4);
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif    foreach($arr as&$value)
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif    $value*=2;
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif    print_r($arr);//Array ( => 2 => 4 => 6 => 8 )
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  4.for循环


http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$arr=array(1,2,3,4,5);
http://xcf007.blog.iyunv.com/images/editer/InBlock.giffor($i=0;$i<count($arr);$i++)
http://xcf007.blog.iyunv.com/images/editer/InBlock.gifecho $arr[$i].'<br/>';
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  
5.each/list

each用法,每次取出个4元素数组,其中0,key对应键;1,value对应值:


http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$arr = array("张三", "李四", "王五");
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$bar = each($arr);
http://xcf007.blog.iyunv.com/images/editer/InBlock.gifprint_r($bar);//Array ( => 张三 => 张三 => 0 => 0 )
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  
list的用法:


http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$arr = array("张三", "李四", "王五");
http://xcf007.blog.iyunv.com/images/editer/InBlock.giflist($a, $a, $a) = $arr;
http://xcf007.blog.iyunv.com/images/editer/InBlock.gifprint_r($a);//Array ( => 王五 => 李四 => 张三 )
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  
list结合each使用:


http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$arr = array("张三"=>20, "李四"=>29, "王五"=>23);
http://xcf007.blog.iyunv.com/images/editer/InBlock.gifwhile(list($key, $value) = each($arr))
http://xcf007.blog.iyunv.com/images/editer/InBlock.gifecho "$key=>$value".'<br/>';
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>


  
注意while(list($key, $value) = each($arr))中each得到的4元素数组中有2个数字索引分别赋给list中2个变量。
所以


http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$arr = array("张三",'b'=>"李四", "王五");
http://xcf007.blog.iyunv.com/images/editer/InBlock.giflist($a, $a) = $arr;
http://xcf007.blog.iyunv.com/images/editer/InBlock.gifprint_r($a);//Array ( => 王五 => 张三 )
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  
6.多维数组


http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$a = array();
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$a['山东'] = "济南";
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$a['山东'] = "威海";
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$a['黑龙江'] = "哈尔滨";
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$a['黑龙江'] = "齐齐哈尔";
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif
foreach($a as$key1=>$v1) {
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif  echo "<b>$key1</b>".'<br/>';
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        foreach($v1 as$v2) {
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif                echo "$v2 \n";
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        }
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        echo '<br/>';
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif}
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  
7.数组函数

key取键值


http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$arr = array(
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        '张三' => '济南',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        '李四' => '烟台',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        '王五' => '威海',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        '赵六' => '北京',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        );
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif
while($person = current($arr)) {
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        if($person == '威海') {
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif                echo key($arr).'<br />';
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        }
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        next($arr);
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif}
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>

  上面会得到 '王五',但是如果烟台为 ""空串,则程序执行到这里会跳出循环,空串转成false。

用for实现foreach效果:

http://xcf007.blog.iyunv.com/images/editer/InBlock.gif<?php
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif$arr = array(
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        '百度' => 'http://www.baidu.com',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        '谷歌' => 'http://www.google.com.hk',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        '400电话' => 'http://www.my400800.cn',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        'bing' => 'http://cn.bing.com',
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif        );
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif
for(reset($arr);$key=key($arr);next($arr))
http://xcf007.blog.iyunv.com/images/editer/InBlock.gifecho $key.'<br/>';
http://xcf007.blog.iyunv.com/images/editer/InBlock.gif?>


  
这个问题同上,如果某个键值计算为false也会跳出循环。
页: [1]
查看完整版本: PHP循环结合数组和list的使用