Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write two-dimensional array in php

Shulou Source: shulou.com Published: 2022-06-03 08:02:10 10月03日 Update

This article shows you how to write a two-dimensional array in php, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Php two-dimensional array to write: 1, direct assignment method, syntax "$array [one-dimensional subscript] [two-dimensional subscript] =" value "; 2, the use of array () function, syntax" array (key= > value...), array (key= > value...).

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Two-dimensional array

A two-dimensional array is declared in the same way as an one-dimensional array, except that one or more elements in the array are also declared as an array. There are also two ways to declare a two-dimensional array by directly assigning values to the array elements and using the array () function.

Let's declare an array by assigning values to array elements directly. The sample code is as follows:

The running results are as follows:

Array ([0] = > Array ([name] = > zhangsan [chinese] = > 89 [math] = > 95 [english] = > 88) [1] = > Array ([name] = > lisi [chinese] = > 91 [math] = > 86 [english] = > 90))

Using the array () function to declare a two-dimensional array is similar to declaring an one-dimensional array. The sample code is as follows: (the following is equivalent to the above code, and the running result is the same)

Similarly, getting the elements in a two-dimensional array is similar to an one-dimensional array, only need to indicate the subscript of each dimension. The example code is as follows:

The running results are as follows:

Zhangsan got 95 points in math.

Tip: different dimensions of the array indicate that we need to use several subscripts (indexes) to get the corresponding array elements, such as two subscripts for a two-dimensional array, three for a three-dimensional array, and so on.

The above is how to write a two-dimensional array in php. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Array two-dimensional element subscript code one-dimensional function example result run content classmate skill mathematics way knowledge dimension grammar different similar Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Docker MySQL Xiaomi