Home > Backend Development > PHP Problem > How to write a two-dimensional array in php

How to write a two-dimensional array in php

青灯夜游
Release: 2023-03-13 07:56:02
Original
8976 people have browsed it

php How to write a two-dimensional array: 1. Direct assignment method, syntax "$array[one-dimensional subscript][two-dimensional subscript]="value";"; 2. Use the array() function, Syntax "array(array(key=>value...),array(key=>value...)....)".

How to write a two-dimensional array in php

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

two-dimensional array

The two-dimensional array is declared in the same way as the one-dimensional array, except that one or more elements in the array are also declared as an array. There are also two declarations: directly assigning values ​​to the array elements and using the array() function. 2 Dimensional array methods.

The following uses the method of directly assigning values ​​​​to the array elements to declare an array. The sample code is as follows:

<?php
    $array[0][&#39;name&#39;] = &#39;zhangsan&#39;;
    $array[0][&#39;chinese&#39;] = &#39;89&#39;;
    $array[0][&#39;math&#39;] = &#39;95&#39;;
    $array[0][&#39;english&#39;] = &#39;88&#39;;
    $array[1][&#39;name&#39;] = &#39;lisi&#39;;
    $array[1][&#39;chinese&#39;] = &#39;91&#39;;
    $array[1][&#39;math&#39;] = &#39;86&#39;;
    $array[1][&#39;english&#39;] = &#39;90&#39;;
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r($array);
?>
Copy after login

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
        )
)
Copy after login

Use the array() function to declare a two-dimensional Arrays are similar to declaring one-dimensional arrays. The sample code is as follows: (The following code is equivalent to the above code, and the running results are the same)

<?php
    $array = array(
        array(&#39;name&#39;=>&#39;zhangsan&#39;,&#39;chinese&#39;=>&#39;89&#39;,&#39;math&#39;=>&#39;95&#39;,&#39;english&#39;=>&#39;88&#39;),
        array(&#39;name&#39;=>&#39;lisi&#39;,&#39;chinese&#39;=>&#39;91&#39;,&#39;math&#39;=>&#39;86&#39;,&#39;english&#39;=>&#39;90&#39;)
    );
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r($array);
?>
Copy after login

Similarly, getting the elements in the two-dimensional array is also the same as the one-dimensional array Similarly, you only need to indicate the subscript of each dimension. The sample code is as follows:

<?php
    $array = array(
        array(&#39;name&#39;=>&#39;zhangsan&#39;,&#39;chinese&#39;=>&#39;89&#39;,&#39;math&#39;=>&#39;95&#39;,&#39;english&#39;=>&#39;88&#39;),
        array(&#39;name&#39;=>&#39;lisi&#39;,&#39;chinese&#39;=>&#39;91&#39;,&#39;math&#39;=>&#39;86&#39;,&#39;english&#39;=>&#39;90&#39;)
    );
    echo $array[0][&#39;name&#39;].&#39;同学的数学考了&#39;.$array[0][&#39;math&#39;].&#39;分&#39;;
?>
Copy after login

The running results are as follows:

zhangsan同学的数学考了95分
Copy after login

Tip: Different dimensions of the array indicate how many dimensions we need to use Subscript (index) is used to obtain the corresponding array element. For example, a two-dimensional array requires two subscripts to obtain the corresponding array element, a three-dimensional array requires three, and so on.

Recommended learning: "PHP Video Tutorial"

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