PHP Development (21)-Use foreach, list, while, each to traverse arrays-PhpStorm

黄舟
Release: 2023-03-06 10:18:01
Original
1835 people have browsed it

foreach:

There are many ways to traverse arrays. In other languages, arrays with consecutive subscripts are generally traversed using for, but based on the special features of PHP arrays Generally, we use foreach, and sometimes each.

First let’s take a look at arrays in most languages: $arr = array("a", "b", "c", "d", "e", "f", "g");

Then let's take a look at the array forms that can exist in PHP: $arr2 = array("a", "b", 100= >"c", "d", "xxx"=>"e", "f", "g");

Yes, arr2 cannot be performed using for Traversal~~So, there is a powerful foreach method~~

foreach has 2 expressions:

1, foreach (array as Custom variable)

2, foreach (array as subscript variable => value variable)


list :

list() assigns a value to a set of variables in one operation. Note that list can only convert consecutively indexed arrays into variables.

I personally feel that list does not have any advantages, except when used with the explode function. explode() explodes a string into an array.

In the following code, an example is demonstrated: list($name, $web) = explode("_",$str);


each:

each() returns the key name and key value of the current element and moves the internal pointer forward.

current() - Returns the value of the current element in the array.

end() - Sets the internal pointer to the last element in the array and outputs it.

next() - Sets the internal pointer to the next element in the array and outputs it.

prev() - Sets the internal pointer to the previous element in the array and outputs it.

reset() - Sets the internal pointer to the first element in the array and outputs it.

Used with while, you can traverse the array: while ($tmp = each($arr)){ print_r($tmp); echo '
'; }

<?php
    /**
     * 遍历数组
     * for只可以遍历$arr这样下标连续的数组
     * foreach可以遍历$arr、$arr2等任何类型的数组
     * foreach的2种用法:
     * 1、foreach(数组 as 自定义变量)
     * 2、foreach(数组 as 下标变量 => 值变量)
     */
    $arr = array("a", "b", "c", "d", "e", "f", "g");
    $arr2 = array("a", "b", 100=>"c", "d", "xxx"=>"e", "f", "g");
    $group = array(
        array("name"=>"iwanghang", "age"=>18, "sex"=>"男", "email"=>"iwanghang@qq.com"),  // $group[0]
        array("name"=>"queen", "age"=>14, "sex"=>"女", "email"=>"queen@qq.com"), // $group[1]
        array("name"=>"king", "age"=>55, "sex"=>"男", "email"=>"king@qq.com"), // $group[2]
    );



    echo &#39;---------- 使用for遍历数组 ----------<br>&#39;;
    for ($i=0; $i<count($arr); $i++){
        echo $arr[$i].&#39;<br>&#39;;
    }
    /* 打印结果:
        a
        b
        c
        d
        e
        f
        g
    */

    echo &#39;---------- 使用foreach遍历数组 ----------<br>&#39;;
    foreach ($arr2 as $value){
        echo $value.&#39;<br>&#39;;
    }
    /* 打印结果:
        a
        b
        c
        d
        e
        f
        g
    */

    echo &#39;---------- 使用foreach遍历数组 2 ----------<br>&#39;;
    foreach ($arr2 as $bb => $vv){
        echo $bb.&#39;-----&#39;.$vv.&#39;<br>&#39;;
    }
    /* 打印结果:
        0-----a
        1-----b
        100-----c
        101-----d
        xxx-----e
        102-----f
        103-----g
    */

    echo &#39;---------- 打印二位数组 ----------<br>&#39;;
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r($group);
    echo &#39;
'; /* 打印结果: Array ( [0] => Array ( [name] => iwanghang [age] => 18 [sex] => 男 [email] => iwanghang@qq.com ) [1] => Array ( [name] => queen [age] => 14 [sex] => 女 [email] => queen@qq.com ) [2] => Array ( [name] => king [age] => 55 [sex] => 男 [email] => king@qq.com ) ) */ echo ''; echo ''; foreach ($group as $kk => $row){ echo ''; if (is_array($row)) { foreach ($row as $col) { echo ''; } }else{ echo ''; } echo '

数组转换为表格

' . $col . ''; } echo '
'; /** * list() 在一次操作中给一组变量赋值。 * 注意,list只能将连续索引的数组转化为变量 * * explode() 把字符串打散为数组。 */ echo '---------- 使用list遍历数组 ----------
'; list($a, $b, $c) = array("郭靖", "黄蓉", "洪七公"); echo $a."
"; // 打印结果:郭靖 echo $b."
"; // 打印结果:黄蓉 echo $c."
"; // 打印结果:洪七公 echo '---------- 使用list遍历数组 2 ----------
'; list($aa, $bb, $cc) = array("郭靖", "two"=>"黄蓉", "洪七公"); echo $aa."
"; // 打印结果:郭靖 echo $bb."
"; // 会报错,因为索引不是连续数组 echo $cc."
"; // 打印结果:洪七公 echo '---------- 使用list遍历数组 3 ----------
'; $str="iwanghang_CSDN"; list($name, $web) = explode("_",$str); echo $name."
"; // 打印结果:iwanghang echo $web."
"; // 打印结果:CSDN /** * each() 返回当前元素的键名和键值,并将内部指针向前移动。 * * 相关的方法: * current() - 返回数组中的当前元素的值 * end() - 将内部指针指向数组中的最后一个元素,并输出 * next() - 将内部指针指向数组中的下一个元素,并输出 * prev() - 将内部指针指向数组中的上一个元素,并输出 * reset() - 将内部指针指向数组中的第一个元素,并输出 */ echo '---------- 使用each遍历数组 ----------
'; $arr3 = ["男主角"=>"郭靖", "黄蓉", "洪七公"]; $people = each($arr3); echo '
&#39;;
    print_r($people);
    echo &#39;
'; /* * 打印结果: Array ( [1] => 郭靖 [value] => 郭靖 [0] => 0 [key] => 0 ) */ $people = each($arr3); print_r($people); // 打印结果:Array ( [1] => 黄蓉 [value] => 黄蓉 [0] => 1 [key] => 1 ) echo "
"; $people = each($arr3); print_r($people); // 打印结果:Array ( [1] => 洪七公 [value] => 洪七公 [0] => 2 [key] => 2 ) echo "
"; $people = each($arr3); print_r($people); // 打印结果:为空 echo "
"; /** * */ echo '---------- 使用while配合each遍历数组 ----------
'; $arr4 = ["男主角"=>"郭靖", "黄蓉", "洪七公", "老顽童", "黄药师"]; while ($tmp = each($arr4)){ print_r($tmp); echo '
'; } /* * 打印结果: Array ( [1] => 郭靖 [value] => 郭靖 [0] => 男主角 [key] => 男主角 ) Array ( [1] => 黄蓉 [value] => 黄蓉 [0] => 0 [key] => 0 ) Array ( [1] => 洪七公 [value] => 洪七公 [0] => 1 [key] => 1 ) Array ( [1] => 老顽童 [value] => 老顽童 [0] => 2 [key] => 2 ) Array ( [1] => 黄药师 [value] => 黄药师 [0] => 3 [key] => 3 ) */ echo '---------- 使用while配合each遍历数组 2 ----------
'; $arr5 = ["男主角"=>"郭靖", "黄蓉", "洪七公", "老顽童", "黄药师"]; while ($tmp = each($arr5)){ //echo "{$tmp['key']}=>{$tmp['value']}"; echo "{$tmp[0]}=>{$tmp[1]}"; // echo "{$tmp['key']}=>{$tmp['value']}"; 相同 echo '
'; } /* * 打印结果: 男主角=>郭靖 0=>黄蓉 1=>洪七公 2=>老顽童 3=>黄药师 */ /** * */ $arr6 = ["one"=>"郭靖", "two"=>"黄蓉", "three"=>"洪七公", "four"=>"老顽童", "five"=>"黄药师"]; echo "当前的位置(默认在第一个):".key($arr6)."=>".current($arr6)."
"; // 当前的位置(默认在第一个):one=>郭靖 next($arr6); next($arr6); next($arr6); echo "当前的位置:".key($arr6)."=>".current($arr6)."
"; // 当前的位置:four=>老顽童 end($arr6); echo "当前的位置:".key($arr6)."=>".current($arr6)."
"; // 当前的位置:five=>黄药师 prev($arr6); prev($arr6); echo "当前的位置:".key($arr6)."=>".current($arr6)."
"; // 当前的位置:three=>洪七公 reset($arr6); echo "当前的位置:".key($arr6)."=>".current($arr6)."
"; // 当前的位置:one=>郭靖
Copy after login

The above is the content of PHP development (21) - using foreach, list, while, and each to traverse arrays - PhpStorm. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


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