Home >Backend Development >PHP Tutorial >PHP array traversal example explanation
This article explains PHP array traversal examples.
Traversing the array
Traversing the array: It means going through each one once
1) for loop
is rarely used because it has defects
ccb9839f1749766605c92d9b79d42e38 Variable 2){ //The statement executed in each loop variable 1 represents the index value of the data currently being experienced (accessed) Variable 1 represents the data currently being experienced (accessed)} bc5574f69a0cba105bc93bd3dc13c4ec
<pre class="brush:java;"> <!--?php /* * foreach来遍历我们的数组 * 这个比较常用,因为是专门为我们来遍历数组的! * */ $arr1=array( 'name'=-->'傻逼', 'num'=>10 ); /* foreach($arr1 as $value){ echo $value.' '; } */ foreach($arr1 as $key=>$value){ echo $key.'=>'.$value.' '; } ?> <!--?php /* * 以后遇到这种情况,咱们到时候再说 - 递归思想的解决 * */ $arr=array( 'a', 'b', 'c', 'd', array( 1,2,3,4,5 ) ); foreach ($arr as $val){ var_dump($val); } ?--> <!--?php /* * 有规律,我们可以直接foreach嵌套去遍历就可以了 * */ $arr=array( array('a','b','c','d'), array('a','b','c','d','e','f'), array('a','b','c','d','f'), array('a','b','c','d'), ); foreach ($arr as $val1){ foreach ($val1 as $val2){ echo $val2.'<br /-->'; } } <!--?php /* * * */ $students=array( array('傻逼',1,true,60.5), array('坑逼',2,true,80.5), array('菜逼',3,false,85.5) ); echo '<table border=1-->'; foreach ($students as $val){ if($val[2]===true){ $val[2]='男'; }else{ $val[2]='女'; } echo "{$val[0]}{$val[1]}{$val[2]}{$val[3]}"; } echo ''; ?>
This article explains PHP array traversal examples. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
PHP implementation of bucket sorting algorithm
##PHP array classification and array creation example explanation
php The use of pdo placeholder (code example explanation)
The above is the detailed content of PHP array traversal example explanation. For more information, please follow other related articles on the PHP Chinese website!