Home > Backend Development > PHP Problem > How to define an empty two-dimensional array in php

How to define an empty two-dimensional array in php

PHPz
Release: 2023-04-23 15:13:43
Original
820 people have browsed it

In PHP programming, it is often necessary to use arrays to store and operate data, and two-dimensional arrays are a commonly used form of arrays. They are composed of multiple one-dimensional arrays and are usually used to store data such as tables and matrices. structure. But sometimes, we may need to define an empty two-dimensional array, so how to define it in PHP?

In PHP, we can define an empty two-dimensional array in two ways, namely through the array() function and the [] array definition syntax.

  1. Use the array() function

In PHP, we can use the array() function to define a two-dimensional array and initialize it to an empty array, as follows Shown:

$two_dim_array = array();
Copy after login

This defines an empty two-dimensional array $two_dim_array.

Next, we can add one-dimensional array elements to $two_dim_array. Before doing this, we need to define the one-dimensional array first, as shown below:

$inner_array = array('a', 'b', 'c');
Copy after login

Then, the defined $inner_array acts as an element of $two_dim_array, as shown below:

$two_dim_array[] = $inner_array;
Copy after login

This adds a new element $inner_array to $two_dim_array, that is, $inner_array is added to $two_dim_array as a subarray.

We can also define multiple one-dimensional arrays and add them to the two-dimensional array, as shown below:

$inner_array1 = array('a', 'b', 'c');
$inner_array2 = array('d', 'e', 'f');
$inner_array3 = array('g', 'h', 'i');
$two_dim_array[] = $inner_array1;
$two_dim_array[] = $inner_array2;
$two_dim_array[] = $inner_array3;

print_r($two_dim_array);
Copy after login

Output result:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
        (
            [0] => d
            [1] => e
            [2] => f
        )

    [2] => Array
        (
            [0] => g
            [1] => h
            [2] => i
        )

)
Copy after login
Copy after login

This defines a three-dimensional array A two-dimensional array with three rows and three columns, $two_dim_array, where each row is a one-dimensional array element.

  1. Use [] array definition syntax

In addition to using the array() function, we can also use [] array definition syntax to define an empty two-dimensional array, as follows Show:

$two_dim_array = array();
$two_dim_array[0] = array();
Copy after login

The $two_dim_array[0] here is equivalent to defining an empty one-dimensional array as the first row of the two-dimensional array. Next, we can add each element to $two_dim_array[0], As shown below:

$two_dim_array[0][] = 'a';
$two_dim_array[0][] = 'b';
$two_dim_array[0][] = 'c';

print_r($two_dim_array);
Copy after login

Output result:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)
Copy after login

This defines a two-dimensional array $two_dim_array with only one row and three columns, where each column is an element.

We can also define a two-dimensional array with multiple rows and multiple columns, as shown below:

$two_dim_array = array();
$two_dim_array[0] = array('a', 'b', 'c');
$two_dim_array[1] = array('d', 'e', 'f');
$two_dim_array[2] = array('g', 'h', 'i');

print_r($two_dim_array);
Copy after login

Output result:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
        (
            [0] => d
            [1] => e
            [2] => f
        )

    [2] => Array
        (
            [0] => g
            [1] => h
            [2] => i
        )

)
Copy after login
Copy after login

This defines a three-row and three-column array. Two-dimensional array $two_dim_array, where each row is a one-dimensional array element.

The above is the detailed content of How to define an empty two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

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