Home  >  Article  >  Backend Development  >  Types of PHP arrays - multidimensional arrays

Types of PHP arrays - multidimensional arrays

黄舟
黄舟Original
2017-05-04 10:04:264373browse

Types of PHP arrays - multidimensional arrays

What is a PHP multidimensional array?

The two articles introduced earlier "Types of PHP arrays-numeric index array" and "Types of PHP arrays-associative arrays" They are all one-dimensional arrays. Today we will introduce multi-dimensional arrays in detail!

Arrays with more than one dimension can be called multi-dimensional arrays

We need to understand that an array is not necessarily a simple list of subscripts and values. In fact, each element in the array The elements can also be another array.

So if the array element in a one-dimensional array is also an array, then it becomes a two-dimensional array.

PHP multi-dimensional array sample code

##Dimensions of the array: two-dimensional

Types of PHP arrays - multidimensional arrays

Define the above two-dimensional array:

<?php
$arr = [[&#39;王刚&#39;, &#39;张丽&#39;, &#39;刘伟&#39;], [&#39;孙丽&#39;, &#39;李强&#39;,&#39;李国庆&#39;], [&#39;赵园园&#39;,&#39;丁丽丽&#39;] ];
echo count($arr); //统计数组的元素个数
echo count($arr, true);
?>

The way to obtain the element "Li Qiang" in the above two-dimensional array is as follows:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr = [[&#39;王刚&#39;, &#39;张丽&#39;, &#39;刘伟&#39;], [&#39;孙丽&#39;, &#39;李强&#39;,&#39;李国庆&#39;], [&#39;赵园园&#39;,&#39;丁丽丽&#39;] ];
echo $arr[1][1]
?>

The output result is :

Types of PHP arrays - multidimensional arrays

Types of PHP arrays - multidimensional arrays

##Dimensions of the array: three-dimensional

Types of PHP arrays - multidimensional arraysMethod to obtain "Li Qiang" and "Liu Jun":

<?php
header("Content-Type:text/html; charset=utf-8");
$arr=[ [[&#39;王刚&#39;,&#39;张丽&#39;,&#39;刘伟&#39;],  [&#39;孙丽&#39;,&#39;李强&#39;,&#39;李国庆&#39;],  [&#39;赵园园&#39;,&#39;丁丽丽&#39;] ], 
[ [&#39;宋红&#39;,&#39;马小丽&#39;], [&#39;张颖&#39;,&#39;刘军&#39;,&#39;黄涛&#39;], [&#39;杜磊&#39;,&#39;朱婷婷&#39;]],];
echo $arr[0][1][1];//获取李强的方式
echo "<br>";
echo $arr[1][1][1];//获取刘军的方式
?>

The output result is:

Types of PHP arrays - multidimensional arrays


【Related tutorial recommendations】

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

The above is the detailed content of Types of PHP arrays - multidimensional arrays. 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