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

How to create a two-dimensional array in php

WBOY
WBOYOriginal
2023-05-07 11:42:10696browse

In PHP, a two-dimensional array refers to an array containing multiple arrays, and each array is also called a one-dimensional array. Such arrays can be used in various scenarios such as data tables and images. This article will introduce how to create a two-dimensional array in PHP.

  1. Directly initialize a two-dimensional array

One way to create a two-dimensional array is to initialize it directly. For example:

$array = array(
    array("apple", "orange", "banana"),
    array("car", "bike", "plane"),
    array("book", "pen", "notebook")
);

The above code will create a $array array, which contains three arrays. Each subarray contains three elements.

  1. Use a for loop and assignment operator to create a two-dimensional array

Another method is to use an assignment operation in a for loop to fill the two-dimensional array:

$rows = 3;
$cols = 3;

$array = array();

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

In the code, we first define the number of rows and columns of the array. Next we define an empty array $array. We then use two for loops to iterate through each array element. In the first loop, we iterate through each row and create an empty array to hold all the elements of that row. In the second loop, we assign values ​​to the columns in each row. This example uses the values ​​of $i and $j and multiplies their values ​​to generate each element in the array.

  1. Use the array_push() function to add a new one-dimensional array

Another method is to use the array_push() function to add a new one-dimensional array to an existing two-dimensional array. A one-dimensional array:

$array = array();
$fruit = array("apple", "orange", "banana");
$vehicle = array("car", "bike", "plane");
$stationery = array("book", "pen", "notebook");
array_push($array, $fruit);
array_push($array, $vehicle);
array_push($array, $stationery);

In this example, we first create an empty array $array. Next, we create three one-dimensional arrays $fruit, $vehicle, and $stationery, and add them to the $array two-dimensional array using array_push() respectively.

Summary:

Two-dimensional array is an important data structure for effective organization and storage of data. In actual application scenarios, different creation methods should be selected according to the actual situation. During the specific writing process, we can select the corresponding code fragments as needed, fill them into our code, and generate a two-dimensional array that meets actual needs.

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