Home > Backend Development > PHP Tutorial > What is a php two-dimensional array? Detailed explanation of php two-dimensional array examples

What is a php two-dimensional array? Detailed explanation of php two-dimensional array examples

黄舟
Release: 2023-03-07 11:26:01
Original
10051 people have browsed it

What is a php two-dimensional array?

A two-dimensional array is also called a matrix. It is essentially an array with arrays as array elements. If the elements of an array are one-dimensional arrays, then we call the array a two-dimensional array.

What is a php two-dimensional array? Detailed explanation of php two-dimensional array examples

Detailed explanation of PHP two-dimensional array examples

Our previous article "Types of PHP arrays-multidimensional arrays" 》There is an explanation of two-dimensional arrays. The text description may make everyone more confused as you read it. Without code examples, it is intuitive. Below we give specific examples to make it clear to everyone!

PHP two-dimensional array code example:

<?php
header("Content-Type:text/html; charset=utf-8");
$atr = array(
    "网站"=>array("PHP","中文","网"),
    "体育用品"=>array("M"=>"足球","N"=>"篮球"),
    "水果"=>array("橙子",8=>"葡萄","苹果")
);               //声明数组
print_r($atr);  //打印输出数组
?>
Copy after login

The output result is:

Array (
[网站] => Array (
[0] => PHP
[1] => 中文
[2] => 网 )
[体育用品] => Array (
[M] => 足球
[N] => 篮球 )
[水果] => Array (
[0] => 橙子
[8] => 葡萄
[9] => 苹果 )
)
Copy after login

Here is the explanation:

Many people saw above why "Apple" is "9" in the table below?

We have mentioned this issue in previous articles. The array subscript starts from 0 by default, and then increases by 1 in sequence. Of course, you can also specify to start from a certain number, because we "Grapes" was defined as 8 earlier, so the subscript of "apple" later is naturally 9. Do you guys understand now?

The above example implements the declaration of a two-dimensional array. According to this idea, you can declare a higher-dimensional array. Isn’t it very simple? If there are some problems with arrays of more than two dimensions, If you don’t understand, you can refer to our previous article "Types of PHP Arrays-Multidimensional Arrays"!

【Related tutorial recommendations】

1. Relevant topic recommendations: "php array (Array)"


The above is the detailed content of What is a php two-dimensional array? Detailed explanation of php two-dimensional array examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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