Home Backend Development PHP Problem How to use two-dimensional array in php

How to use two-dimensional array in php

Apr 25, 2023 am 09:01 AM

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

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

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>';
}
Copy after login

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

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

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

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24