"Tom", "age"=>18, "score"=>85), array(""/> "Tom", "age"=>18, "score"=>85), array("">

Home  >  Article  >  Backend Development  >  How to traverse and output a two-dimensional array in php

How to traverse and output a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-14 18:38:531696browse

In PHP, a two-dimensional array refers to an array containing another array. It can be understood as a table, in which each row is a small array, and each small array is composed of multiple key-value pairs. Therefore, outputting a two-dimensional array requires traversing each of its small arrays and outputting their key-value pairs.

Suppose there is a two-dimensional array as follows:

$students = array(
    array("name"=>"Tom", "age"=>18, "score"=>85),
    array("name"=>"Jerry", "age"=>19, "score"=>90),
    array("name"=>"Bob", "age"=>18, "score"=>80),
);

Next, we traverse the two-dimensional array through two nested foreach loops and output the key value of each small array. Right:

foreach($students as $student){
    foreach($student as $key=>$value){
        echo $key . ":" . $value . " ";
    }
    echo "<br/>";
}

In the above code, the outer foreach traverses each small array $student in the $students array, while the inner foreach traverses the key-value pairs of each small array. Each time a key-value pair is traversed, they are output through the echo statement, separated by ":", and finally output with a space. After the inner foreach loop is completed, a newline character "
" is output, so that when the results are displayed on the web page, each small array occupies one line and is easy to view.

Execute the above code, we can see the following output:

name:Tom age:18 score:85 
name:Jerry age:19 score:90 
name:Bob age:18 score:80

In addition to the above method, we can also use the for loop to output each small array in the two-dimensional array one by one. The following is the corresponding code example:

for($i=0; $i<count($students); $i++){
    for($j=0; $j<count($students[$i]); $j++){
        echo key($students[$i]) . ":" . current($students[$i]) . " ";
        next($students[$i]);
    }
    echo "<br/>";
    reset($students[$i]);
}

The advantage of using a for loop is that you can grasp the order of traversal more intuitively, and using the next and reset functions at the same time, you can operate each key-value pair more conveniently. .

To sum up, the methods for outputting PHP two-dimensional arrays include traversal method and for loop method. The former code is simple and easy to understand, while the latter masters algorithmic thinking and can operate arrays more flexibly. In actual development, choosing an appropriate method to output a two-dimensional array according to the specific situation and code requirements can make the code more standardized and efficient.

The above is the detailed content of How to traverse and output a two-dimensional array in php. 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