Home >Backend Development >PHP Tutorial >PHP array traversal example explanation

PHP array traversal example explanation

jacklove
jackloveOriginal
2018-05-22 17:54:042098browse

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(
        &#39;name&#39;=-->&#39;傻逼&#39;,
        &#39;num&#39;=>10
);
/*
foreach($arr1 as $value){
    echo $value.&#39;
&#39;;
}
*/
foreach($arr1 as $key=>$value){
    echo $key.&#39;=>&#39;.$value.&#39;
&#39;;
}
?>   
<!--?php
/*
 * 以后遇到这种情况,咱们到时候再说 - 递归思想的解决
 * */
$arr=array(
    &#39;a&#39;,
    &#39;b&#39;,
    &#39;c&#39;,
    &#39;d&#39;,
    array(
        1,2,3,4,5
    )
);
foreach ($arr as $val){
    var_dump($val);
}
?-->  
<!--?php
/*
 * 有规律,我们可以直接foreach嵌套去遍历就可以了
 * */
$arr=array(
    array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;),
    array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;),
    array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;f&#39;),
    array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;),
);
foreach ($arr as $val1){
    foreach ($val1 as $val2){
        echo $val2.&#39;<br /-->&#39;;
    }
}
<!--?php
/*
 *
 * */
$students=array(
    array(&#39;傻逼&#39;,1,true,60.5),
    array(&#39;坑逼&#39;,2,true,80.5),
    array(&#39;菜逼&#39;,3,false,85.5)
);
echo &#39;<table border=1-->&#39;;
foreach ($students as $val){
    if($val[2]===true){
        $val[2]=&#39;男&#39;;
    }else{
        $val[2]=&#39;女&#39;;
    }
    echo "{$val[0]}{$val[1]}{$val[2]}{$val[3]}";
}
echo &#39;&#39;;
?>

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!

Statement:
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