Home > Backend Development > PHP Problem > How to output the value of a two-dimensional array in php

How to output the value of a two-dimensional array in php

王林
Release: 2023-05-19 13:24:08
Original
1065 people have browsed it

In PHP programming, two-dimensional array is a very common data structure. When dealing with this data structure, we need to know how to output its corresponding value. This article will introduce readers to how to output values ​​in a two-dimensional array using different methods and techniques.

1. Use foreach loop

The foreach loop is a commonly used loop structure in PHP and is also an effective method for outputting values ​​​​in a two-dimensional array. The following is an example of using a foreach loop to output a two-dimensional array:

$students = array(
    array('name' => 'Tom', 'score' => 80),
    array('name' => 'Jerry', 'score' => 90),
    array('name' => 'Alice', 'score' => 85)
);

foreach ($students as $key => $value) {
    echo "第" . ($key + 1) . "个学生的信息如下:" . "<br />";
    echo "姓名:" . $value['name'] . "<br />";
    echo "分数:" . $value['score'] . "<br /><br />";
}
Copy after login

The output results are as follows:

第1个学生的信息如下:
姓名:Tom
分数:80

第2个学生的信息如下:
姓名:Jerry
分数:90

第3个学生的信息如下:
姓名:Alice
分数:85
Copy after login

In this example, we first define an object named $students A two-dimensional array containing the information of three students. We then use a foreach loop to iterate through this array and output the name and score of each student in the loop.

2. Use the for loop

In addition to the foreach loop, we can also use the for loop to output the values ​​​​in the two-dimensional array. The following is an example of using a for loop to output a two-dimensional array:

$students = array(
    array('name' => 'Tom', 'score' => 80),
    array('name' => 'Jerry', 'score' => 90),
    array('name' => 'Alice', 'score' => 85)
);

$row = count($students);
$col = count($students[0]);

for ($i = 0; $i < $row; $i++) {
    echo "第" . ($i + 1) . "个学生的信息如下:" . "<br />";
    for ($j = 0; $j < $col; $j++) {
        echo key($students[$i]) . ":" . current($students[$i]) . "<br />";
        next($students[$i]);
    }
    echo "<br />";
}
Copy after login

The output result is the same as the previous example.

In this example, we use a for loop to traverse the two-dimensional array $students. First, we get the number of rows and columns of the array and use an outer loop to iterate over each row and an inner loop to iterate over each column. In the inner loop, we use the PHP functions key and current to obtain the key and value of the current column respectively, and use the next function to print out the corresponding value.

3. Use the array_walk function

array_walk The function is a flexible array iteration function in PHP that can help us output the values ​​in a two-dimensional array. The following is an example of using the array_walk function to output a two-dimensional array:

$students = array(
    array('name' => 'Tom', 'score' => 80),
    array('name' => 'Jerry', 'score' => 90),
    array('name' => 'Alice', 'score' => 85)
);

function display_student_info($value, $key)
{
    echo $key . ":" . $value . "<br />";
}

foreach ($students as $key => $value) {
    echo "第" . ($key + 1) . "个学生的信息如下:" . "<br />";
    array_walk($value, 'display_student_info');
    echo "<br />";
}
Copy after login

The output is the same as the previous example.

In this example, we first define a two-dimensional array named $students, and then define a function named display_student_info for output Information about each student. Next, we use a foreach loop to traverse the two-dimensional array, and call the array_walk function in the loop, apply the function display_student_info to each student's information, and output the student's name and score. .

4. Use the print_r function or var_dump function

We can also use the print_r function and the var_dump function to output the values ​​in the two-dimensional array. The following is an example of using the print_r function to output a two-dimensional array:

$students = array(
    array('name' => 'Tom', 'score' => 80),
    array('name' => 'Jerry', 'score' => 90),
    array('name' => 'Alice', 'score' => 85)
);

print_r($students);
Copy after login

The output results are as follows:

Array
(
    [0] => Array
        (
            [name] => Tom
            [score] => 80
        )

    [1] => Array
        (
            [name] => Jerry
            [score] => 90
        )

    [2] => Array
        (
            [name] => Alice
            [score] => 85
        )
)
Copy after login

var_dump The method of using the function is the same as print_r The function is similar, except that the output results will be more detailed.

Summary

This article introduces how to output the value of a two-dimensional array in PHP. It mainly introduces several common methods and techniques such as using foreach loop, for loop, array_walk function, print_r function and var_dump function. In actual development, we can choose the appropriate method to output the value of the two-dimensional array according to the specific situation to improve code efficiency and readability.

The above is the detailed content of How to output the value of a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

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