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

How to store two-dimensional array in php

PHPz
PHPzOriginal
2023-04-20 15:01:52564browse

In PHP, two-dimensional arrays are a very common data type, usually used to store tabular or matrix data. A two-dimensional array is an array composed of multiple one-dimensional arrays. It is usually represented in a program as a nested array, where each sub-array represents a one-dimensional array. In this article, we will discuss how to store two-dimensional arrays in PHP and demonstrate how to manipulate these arrays through examples.

1. Create a two-dimensional array

Creating a two-dimensional array in PHP is very simple. You only need to create a nested array when defining the array. The code is as follows:

$matrix = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

The above code creates a two-dimensional array containing three one-dimensional arrays, each one-dimensional array containing three integers. We can also use a for loop to create a two-dimensional array with multiple rows and columns. The example is as follows:

$rows = 4;
$cols = 5;
$matrix = array();
for ($i = 0; $i < $rows; $i++) {
    $row = array();
    for ($j = 0; $j < $cols; $j++) {
        $row[] = $i * $cols + $j;
    }
    $matrix[] = $row;
}

In the above code, we use the for loop to create a two-dimensional array containing 4 rows and 5 columns. And store the row and column index values ​​in the array.

2. Accessing two-dimensional arrays

Accessing two-dimensional arrays in PHP is very simple. You only need to use square brackets to specify the index values ​​of the rows and columns you want to access. For example, to access the second row and third column elements of the $matrix array created above, you can use the following code:

echo $matrix[1][2];

The above code will output the second row and third column element 6 of the $matrix array.

We can also use the for loop to traverse the entire two-dimensional array, the example is as follows:

$rows = count($matrix);
$cols = count($matrix[0]);
for ($i = 0; $i < $rows; $i++) {
    for ($j = 0; $j < $cols; $j++) {
        echo $matrix[$i][$j] . &#39; &#39;;
    }
    echo &#39;<br>';
}

In the above code, we use the for loop to traverse the entire two-dimensional array $matrix, and use the echo statement Output the array elements to the page in sequence.

3. Operating two-dimensional arrays

It is also very convenient to operate two-dimensional arrays in PHP. We can use various array functions to operate two-dimensional arrays. The following are some commonly used operation functions:

  1. array_push(): Add one element or multiple elements to the end of a two-dimensional array.
$row = array(10, 11, 12);
array_push($matrix, $row);

The above code adds a new one-dimensional array to the end of the $matrix array.

  1. array_pop(): Remove and return the last element of the array.
$row = array_pop($matrix);

The above code deletes and returns the last one-dimensional array of the $matrix array.

  1. array_shift(): Remove and return the first element of the array.
$row = array_shift($matrix);

The above code removes and returns the first one-dimensional array of the $matrix array.

  1. array_unshift(): Adds one or more elements to the beginning of the array.
$row = array(-1, 0, 1);
array_unshift($matrix, $row);

The above code adds a new one-dimensional array to the beginning of the $matrix array.

  1. array_slice(): Remove a segment of elements from the array.
$row = array_slice($matrix, 1, 2);

The above code takes two consecutive elements starting from the second element from the $matrix array and puts them into a new array $row.

4. Summary

Storing two-dimensional arrays in PHP is very simple. You only need to create a nested array when defining the array. It is also very convenient to access and operate two-dimensional arrays. You can use various array functions to operate on two-dimensional arrays. In actual development, two-dimensional array is a very commonly used data structure, and it is very important to master its usage.

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