Home > Article > Backend Development > Types of PHP arrays - multidimensional arrays
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
Define the above two-dimensional array:<?php $arr = [['王刚', '张丽', '刘伟'], ['孙丽', '李强','李国庆'], ['赵园园','丁丽丽'] ]; 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 = [['王刚', '张丽', '刘伟'], ['孙丽', '李强','李国庆'], ['赵园园','丁丽丽'] ]; echo $arr[1][1] ?>The output result is :
##Dimensions of the array: three-dimensional
Method to obtain "Li Qiang" and "Liu Jun":
<?php header("Content-Type:text/html; charset=utf-8"); $arr=[ [['王刚','张丽','刘伟'], ['孙丽','李强','李国庆'], ['赵园园','丁丽丽'] ], [ ['宋红','马小丽'], ['张颖','刘军','黄涛'], ['杜磊','朱婷婷']],]; echo $arr[0][1][1];//获取李强的方式 echo "<br>"; echo $arr[1][1][1];//获取刘军的方式 ?>
The output result is:
【Related tutorial recommendations】
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!