PHP multidimensional array
In the previous tutorial, we have learned that an array is a simple list of numbers/values.
Sometimes we want to use more than one key to save data, so we need to use PHP's multi-dimensional array to achieve it.
Basic knowledge
##PHP multidimensional array refers to a More than one array PHP can understand it as a multi-dimensional array of two, three, four or five levels or even more levels. However, most people have trouble managing arrays that are more than three levels deep
Note: The dimension of the array indicates the index number of elements that need to be selected
php two-dimensional array
A two-dimensional array is an array of arrays. Similarly, a three-dimensional array is an array of arrays.Note:A two-dimensional array requires two indexes to select elements
Example
Suppose there is a score sheet
We can save the array in the above table in a two-dimensional array,
$arr=array(
array("Xiao Ming" ,"90","80","77"),
array("小龙","88","75","89"),
array("小花","99"," 95","94"),
);
Use the code to output the result:
"; echo $arr[1][0]."---语文:".$arr[1][1].":数学:".$arr[1][2].":英语:".$arr[1][3]."
"; echo $arr[2][0]."---语文:".$arr[2][1].":数学:".$arr[2][2].":英语:".$arr[2][3]."
"; ?>
The program running result:
Xiaoming---Chinese: 90: Mathematics: 80: English: 77
Xiaolong---Chinese: 88: Mathematics: 75: English: 89
Xiaohua---Chinese: 99: Mathematics: 95: English: 94
We can also use another for loop inside a for loop to get the elements in the array
Example
行数$x"; echo"
- "; for($row=0;$row<3;$row++){ echo "
- ".$arr[$x][$row]." "; } echo"
Program running result:
Number of lines 0
• Xiao Ming
• 90
• 80
Line number 1
• Xiaolong
• 88
• 75
Line number 2
• Xiaohua
• 99
• 95
##PHP three-dimensional array
Note:Two-dimensional array requires two indexes to select elements
Example
Program running result:
laya
- Course Recommendations
- Courseware download
-
IntermediateFront-end Vue3 actual combat [handwritten vue project]
2857 people are watching -
ElementaryAPIPOST tutorial [Popularization of technical concepts related to network communication]
1795 people are watching -
IntermediateIssue 22_Comprehensive actual combat
5521 people are watching -
ElementaryIssue 22_PHP Programming
5172 people are watching -
ElementaryIssue 22_Front-end development
8713 people are watching -
IntermediateBig data (MySQL) video tutorial full version
4525 people are watching -
ElementaryGo language tutorial-full of practical information and no nonsense
2794 people are watching -
ElementaryGO Language Core Programming Course
2814 people are watching -
IntermediateJS advanced and BootStrap learning
2563 people are watching -
IntermediateSQL optimization and troubleshooting (MySQL version)
3374 people are watching -
IntermediateRedis+MySQL database interview tutorial
2963 people are watching -
ElementaryDeliver food or learn programming?
5708 people are watching
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
Name | ## Language |
Mathematics | English |
90 |
80 | 77 | |
88 | 75 | 89 | ## 小花 |
95 | 94 |