Explain relevant examples of PHP array traversal

jacklove
Release: 2023-03-30 17:40:01
Original
1992 people have browsed it

Traverse the array: It means going through each one once. This article will explain the relevant examples.

1) for loop

is rarely used because it has defects

<!--?php
/*
 使用for循环可以遍历数组
*/
/*
count(数组[,1])返回数组里面数据的个数,还可以获取多维数组的个数 ,当然一般我们只传一个数组进去就可以了
$arr1=array(
        array(1,2,3),
        array(4,5,6)
);
echo count($arr1,1);//2+6=8
*/
$arr=array(
    &#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;
);
for($i=0;$i<count($arr);$i++){
    echo $arr[$i];
}

2)foreach语句来实现遍历数组的目的 foreach(数组变量 as 变量1){ //每次循环执行的语句 变量1代表当前正在经历(访问)的数据 } foreach(数组变量 as 变量1=>变量2){ //每次循环执行的语句 变量1代表当前正在经历(访问)的数据的索引值 变量1代表当前正在经历(访问)的数据 }

<!--?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;;
}
?>
Copy after login
<!--?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);
}
?-->
Copy after login
<!--?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;;
?>
Copy after login

This article explains related examples of PHP array traversal, more For related content, please pay attention to php Chinese website.

Related recommendations:

Explain PHP array classification and array creation methods

Explain the predefined superglobal of PHP arrays Array variables

Explanation on PHP language tags, command separators, and comments


The above is the detailed content of Explain relevant examples of PHP array traversal. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template