Questions about php foreach reference
typecho
typecho 2017-07-04 13:45:55
0
3
719

Foreach uses & to traverse the array arr2, and then traverses the array again. The result obtained is very confusing. I wonder if any expert can explain how the & traversal pointer moves.
code show as below:

    $arr2 = ['a','s','d'];
    foreach ($arr2 as $k => &$v){
        echo $k." ".$v."<br>";
    }
    //unset($v);
    foreach ($arr2 as $k => $v){
        echo $k." ";
        echo $v." ".current($arr2)."<br>";
    }

Result:
0 a
1 s
2 d
0 a a
1 s a
2 s a

Why does the pointer stop when it moves to s during the second traversal?

typecho
typecho

Following the voice in heart.

reply all(1)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!