PHP 2D Array: Definition & Assignment

To define and assign values to a 2D array in PHP, you can achieve this by the following method:

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

// 访问二维数组中的元素
echo $two_dim_array[0][0]; // 输出 1
echo $two_dim_array[1][2]; // 输出 6
echo $two_dim_array[2][1]; // 输出 8

The code above creates a 3×3 two-dimensional array with values ranging from 1 to 9. Elements in the array can be accessed by specifying their row and column indexes.

bannerAds