Home  >  Article  >  Backend Development  >  How to use two-dimensional array in php

How to use two-dimensional array in php

PHPz
PHPzOriginal
2023-04-25 09:01:56461browse

Two-dimensional array is to store multiple arrays in one array. In PHP, you can use arrays to store multiple key-value pairs, which can be integers, strings, Boolean, etc.

Let’s take a look at how to define a two-dimensional array in PHP and how to access its values.

Define a two-dimensional array

In PHP, defining a two-dimensional array is very simple, you just need to include another array in the array. Such as the following example:

// 定义一个二维数组
$myArray = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

The above code defines a two-dimensional array containing 3 arrays, each array contains 3 elements. As you can see, each array is enclosed by a bracket, and then these arrays are enclosed by a curly bracket.

Accessing two-dimensional arrays

Accessing values ​​in two-dimensional arrays is also very simple and can be accessed using subscripts. For example:

// 访问二维数组中的值
echo $myArray[0][0]; // 输出 1
echo $myArray[1][1]; // 输出 5
echo $myArray[2][2]; // 输出 9

In the above code, $myArray[0][0] means accessing the first element of the first array, which is 1. In the same way, $myArray[1][1] accesses the second element of the second array, which is 5.

Traversing a two-dimensional array

Traversing a two-dimensional array is also very simple. You can use the foreach statement provided by PHP. Such as the following example:

// 遍历二维数组
foreach ($myArray as $item) {
    foreach ($item as $value) {
        echo $value . ' ';
    }
    echo '<br>';
}

The above code outputs all the values ​​in the entire two-dimensional array. The values ​​​​of each array are separated by spaces. After the value of each array is output, the line breaks.

Commonly used two-dimensional array functions

In addition to the basic operations introduced above, PHP also provides many commonly used two-dimensional array functions. Some of them are described below.

  1. count Function

count The function is used to count the number of one-dimensional arrays in a two-dimensional array. Its syntax It is:

$count = count($myArray);

This function returns the number of one-dimensional arrays contained in $myArray, which is 3.

  1. array_push Function

array_push The function is used to add a one-dimensional array to the end of a two-dimensional array. The syntax is:

$lastArray = array(10, 11, 12);
array_push($myArray, $lastArray);

This function adds a new array containing 3 elements to the end of the two-dimensional array.

  1. array_column Function

array_column The function is used to return the value of the specified column in the two-dimensional array. Its syntax is :

$newArray = array_column($myArray, 1);

This function returns a new array containing the elements in the second column of $myArray.

Summary

Two-dimensional array is a very commonly used data type in PHP, which can store more complex data structures. When using two-dimensional arrays, we need to pay attention to how subscripts of different dimensions are accessed. Mastering the use of two-dimensional arrays and commonly used functions can allow us to process and operate data more easily.

The above is the detailed content of How to use 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