Home > Article > Backend Development > PHP Development (21)-Use foreach, list, while, each to traverse arrays-PhpStorm
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() 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() 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 '0c6dc11e160d3b678d68754cc175188a'; }
<?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 '---------- 使用for遍历数组 ----------<br>'; for ($i=0; $i<count($arr); $i++){ echo $arr[$i].'<br>'; } /* 打印结果: a b c d e f g */ echo '---------- 使用foreach遍历数组 ----------<br>'; foreach ($arr2 as $value){ echo $value.'<br>'; } /* 打印结果: a b c d e f g */ echo '---------- 使用foreach遍历数组 2 ----------<br>'; foreach ($arr2 as $bb => $vv){ echo $bb.'-----'.$vv.'<br>'; } /* 打印结果: 0-----a 1-----b 100-----c 101-----d xxx-----e 102-----f 103-----g */ echo '---------- 打印二位数组 ----------<br>'; echo '<pre class="brush:php;toolbar:false">'; print_r($group); echo ''; /* 打印结果: 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 '
' . $col . ' | '; } }else{ echo ''; } echo ' |
'; print_r($people); echo ''; /* * 打印结果: Array ( [1] => 郭靖 [value] => 郭靖 [0] => 0 [key] => 0 ) */ $people = each($arr3); print_r($people); // 打印结果:Array ( [1] => 黄蓉 [value] => 黄蓉 [0] => 1 [key] => 1 ) echo "
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 (www.php.cn)!