Home > Hosting > Program

How to build a two-dimensional array in PHP

2024-03-28 14:23:23

In PHP, array is an important data type, which can be used to store and manage data. Specifically, an array is a data structure that can store multiple values, and it can be a one-dimensional, two-dimensional or higher-dimensional structure. This article will introduce how to build a two-dimensional array in PHP.

A two-dimensional array is an array composed of rows of elements, which can be regarded as a collection of several one-dimensional arrays. In PHP, you can build a two-dimensional array in the following ways:

$array = array( array(value1, value2, value3), array(value4, value5, value6), array(value7, value8, value9));

Among them, each subarray is called one-dimensional array, the first dimension represents the subscripts of subarrays, and the second dimension represents the subscripts of elements in subarrays. For example, $array0 represents the first element of the first subarray in a two-dimensional array.

The following is a concrete example. Suppose we want to construct a two-dimensional array to represent the transcripts of several students, in which each subarray contains the students' names, math scores and Chinese scores. It can be realized by the following code:

$name1 = "Zhang San"; $name2 = "Li Si"; $name3 = "Wang Wu"; $math_score1 = 80; $math_score2 = 90; $math_score3 = 70; $chinese_score1 = 90; $chinese_score2 = 85; $chinese_score3 = 95; $scores = array( array("name"=>$name1, "math"=>$math_score1, "chinese"=>$chinese_score1), array("name"=>$name2, "math"=>$math_score2, "chinese"=>$chinese_score2), array("name"=>$name3, "math"=>$math_score3, "chinese"=>$chinese_score3));

In the above code, we define the names of three students and their math and Chinese scores, and store them in different variables. Then, we encapsulate this information into subarrays, and treat each subarray as an element of a two-dimensional array. Finally, we assign the two-dimensional array to the $scores variable.

By accessing the elements in the two-dimensional array, we can get the information of each student. For example, to get the math score of the second student, you can use the following code:

$math_score2 = $scores[1]["math"];

Among them, $scores[1] represents the second sub-array in the two-dimensional array, and ["math"] represents the mathematical achievement elements in this sub-array.

Summary:

Two-dimensional array is one of the commonly used data types in PHP, which can conveniently manage multiple elements. Two-dimensional arrays can be constructed by defining integer arrays, string arrays and mixed arrays. You can access the elements in a two-dimensional array by subscripting. In practical development, two-dimensional arrays are widely used in the display of table data and the storage of database operation results. I hope readers can understand the basic usage of two-dimensional array through this article and better apply PHP programming.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us