Android programmers learn PHP development (21) - traverse arrays using foreach, list, while, and each - PhpStorm

Keywords: PHP Mobile Android

For reprinting, please indicate the source: http://blog.csdn.net/iwanghang/
Feel that the blog is useful, please comment, please pay attention, thank you! ~


foreach:

There are many ways to traverse arrays. In other languages, subscript consecutive arrays are generally traversed by for, but because of the particularity of php arrays, we usually use for each, sometimes each.

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

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

Yes, arr2 can't use for to traverse ~, so there's a powerful foreach way ~

foreach has two expressions:

1. foreach (array as custom variable)

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


list:

list() assigns a set of variables in one operation. Note that lists can only convert arrays of consecutive indexes into variables.

Personally, I don't think list has any advantages, except when it works with explode functions. explode() splits strings into arrays.

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


each:

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

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

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

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

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

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

With while, you can traverse arrays: while ($tmp = each ($arr){print_r($tmp); > echo'< br >';}



<?php
    /**
     * foreach
     * for You can only traverse arrays with subscripts like $arr
     * foreach You can traverse any type of array like $arr, $arr2, etc.
     * foreach Two uses:
     * 1,foreach(Array as custom variables)
     * 2,foreach(Array as subscript variable=> value variable
     */
    $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"=>"male", "email"=>"iwanghang@qq.com"),  // $group[0]
        array("name"=>"queen", "age"=>14, "sex"=>"female", "email"=>"queen@qq.com"), // $group[1]
        array("name"=>"king", "age"=>55, "sex"=>"male", "email"=>"king@qq.com"), // $group[2]
    );



    echo '---------- Use for foreach ----------<br>';
    for ($i=0; $i<count($arr); $i++){
        echo $arr[$i].'<br>';
    }
    /* Print results:
        a
        b
        c
        d
        e
        f
        g
    */

    echo '---------- Use foreach foreach ----------<br>';
    foreach ($arr2 as $value){
        echo $value.'<br>';
    }
    /* Print results:
        a
        b
        c
        d
        e
        f
        g
    */

    echo '---------- Use foreach Traversal Array 2 ----------<br>';
    foreach ($arr2 as $bb => $vv){
        echo $bb.'-----'.$vv.'<br>';
    }
    /* Print results:
        0-----a
        1-----b
        100-----c
        101-----d
        xxx-----e
        102-----f
        103-----g
    */

    echo '---------- Print a binary array ----------<br>';
    echo '<pre>';
    print_r($group);
    echo '</pre>';
    /* Print results:
        Array
        (
            [0] => Array
                (
                    [name] => iwanghang
                    [age] => 18
                    [sex] => male
                    [email] => iwanghang@qq.com
                )

            [1] => Array
                (
                    [name] => queen
                    [age] => 14
                    [sex] => female
                    [email] => queen@qq.com
                )

            [2] => Array
                (
                    [name] => king
                    [age] => 55
                    [sex] => male
                    [email] => king@qq.com
                )

        )
    */

    echo '<table border="1" width="800" align="center">';
    echo '<caption><h1>Converting arrays to tables</h1></caption>';

    foreach ($group as $kk => $row){
        echo '<tr>';
        if (is_array($row)) {
            foreach ($row as $col) {
                echo '<td>' . $col . '</td>';
            }
        }else{
            echo '<td colspan="4"'.$kk.':'.$row.'</td>';
        }
        echo '</tr>';
    }
    echo '</table>';

    /**
     * list() Assign values to a set of variables in a single operation.
     * Note that a list can only convert an array of consecutive indexes into variables
     *
     * explode() Break up strings into arrays.
     */
    echo '---------- Use list foreach ----------<br>';
    list($a, $b, $c) = array("Guo Jing", "Huang Rong", "Master Hongqi");
    echo $a."<br>"; // Print result: Guo Jing
    echo $b."<br>"; // Print result: Huangrong
    echo $c."<br>"; // Print result: Hong Qigong

    echo '---------- Use list Traversal Array 2 ----------<br>';
    list($aa, $bb, $cc) = array("Guo Jing", "two"=>"Huang Rong", "Master Hongqi");
    echo $aa."<br>"; // Print result: Guo Jing
    echo $bb."<br>"; // Errors are reported because indexes are not contiguous arrays
    echo $cc."<br>"; // Print result: Hong Qigong

    echo '---------- Use list Traversal array 3 ----------<br>';
    $str="iwanghang_CSDN";
    list($name, $web) = explode("_",$str);
    echo $name."<br>"; // Print result: iwanghang
    echo $web."<br>"; // Print results: CSDN

    /**
     * each() Returns the key name and value of the current element and moves the internal pointer forward.
     *
     * Relevant methods:
     * current() - Returns the value of the current element in the array
     * end() - Point the internal pointer to the last element in the array and output
     * next() - Point the internal pointer to the next element in the array and output
     * prev() - Point the internal pointer to the previous element in the array and output
     * reset() - Point the internal pointer to the first element in the array and output
     */
    echo '---------- Use each foreach ----------<br>';
    $arr3 = ["Hero"=>"Guo Jing", "Huang Rong", "Master Hongqi"];
    $people = each($arr3);
    echo '<pre>';
    print_r($people);
    echo '</pre>';
    /*
     * Print results:
            Array
            (
                [1] => Guo Jing
                [value] => Guo Jing
                [0] => 0
                [key] => 0
            )
     */
    $people = each($arr3);
    print_r($people); // Print result: Array ([1]=> Huangrong [value]=> Huangrong [0]=> 1 [key]=> 1)
    echo "<br>";
    $people = each($arr3);
    print_r($people); // Print result: Array ([1]=> Hong Qigong [value]=> Hong Qigong [0]=> 2 [key]=> 2)
    echo "<br>";
    $people = each($arr3);
    print_r($people); // Print result: empty
    echo "<br>";

    /**
     *
     */
    echo '---------- Use while Coordination each foreach ----------<br>';
    $arr4 = ["Hero"=>"Guo Jing", "Huang Rong", "Master Hongqi", "Old naughty boy", "Pharmacist Hwang"];
    while ($tmp = each($arr4)){
        print_r($tmp);
        echo '<br>';
    }
    /*
     * Print results:
            Array ( [1] => Guo Jing [value]=> Guo Jing [0]=> male protagonist [key]=> male protagonist
            Array ( [1] => Huangrong [value]=> Huangrong [0]=> 0 [key]=> 0)
            Array ( [1] => Hong Qigong [value]=> Hong Qigong [0]=> 1 [key]=> 1)
            Array ( [1] => Old Naughty Child [value]=> Old Naughty Child [0]=> 2 [key]=> 2)
            Array ( [1] => Huang Pharmacist [value]=> Huang Pharmacist [0]=> 3 [key]=> 3
     */

    echo '---------- Use while Coordination each Traversal Array 2 ----------<br>';
    $arr5 = ["Hero"=>"Guo Jing", "Huang Rong", "Master Hongqi", "Old naughty boy", "Pharmacist Hwang"];
    while ($tmp = each($arr5)){
        //echo "{$tmp['key']}=>{$tmp['value']}";
        echo "{$tmp[0]}=>{$tmp[1]}"; // Echo "{tmp ['key']}=> {tmp ['value']}"; same
        echo '<br>';
    }
    /*
     * Print results:
            Male protagonist => Guo Jing
            0=>Huang Rong
            1=>Master Hongqi
            2=>Old naughty boy
            3=>Pharmacist Hwang
     */

    /**
     *
     */
    $arr6 = ["one"=>"Guo Jing", "two"=>"Huang Rong", "three"=>"Master Hongqi", "four"=>"Old naughty boy", "five"=>"Pharmacist Hwang"];
    echo "Current position(Default in the first):".key($arr6)."=>".current($arr6)."<br>"; // Current location (default in the first): one=> Guo Jing
    next($arr6);
    next($arr6);
    next($arr6);
    echo "Current position:".key($arr6)."=>".current($arr6)."<br>"; // Current Position: 4=> Old Naughty Child
    end($arr6);
    echo "Current position:".key($arr6)."=>".current($arr6)."<br>"; // Current position: 5=> Pharmacist Huang
    prev($arr6);
    prev($arr6);
    echo "Current position:".key($arr6)."=>".current($arr6)."<br>"; // Current position: three=> Hong Qigong
    reset($arr6);
    echo "Current position:".key($arr6)."=>".current($arr6)."<br>"; // Current position: one => Guo Jing



For reprinting, please indicate the source: http://blog.csdn.net/iwanghang/



Welcome mobile development enthusiasts exchange
Shenyang or its surrounding cities are interested in developing Android. Please contact me.
Contact information

Wechat: iwanghang
QQ: 413711276
Mailbox: iwanghang@qq.com



Feel that the blog is useful, please comment, please pay attention, thank you! ~

Posted by lovasco on Tue, 08 Jan 2019 10:48:10 -0800