It is very common to use arrays in PHP programming, and two-dimensional arrays are even more inevitable. A two-dimensional array means that each element in the array is an array, and these array elements are organized by a common key. When using a two-dimensional array, the method of obtaining values is slightly different. Let's take a look at how to get the value of a two-dimensional array in PHP.
1. What is a two-dimensional array?
In PHP, the array created through the array() function can contain multiple values, and each value has a key that represents the value. These key-value pairs can be strings or numbers.
When the array elements themselves are arrays, this is a two-dimensional array. The arrays inside these can continue to contain arrays (three-dimensional arrays, four-dimensional arrays, etc.) to form higher-dimensional arrays.
For example, the following is a simple array of three key-value pairs:
$fruits = array("apple", "banana", "cherry");
This array contains three values, each value corresponds to a key. Key-value pairs can be accessed as follows:
echo $fruits[0]; // 输出 apple echo $fruits[1]; // 输出 banana echo $fruits[2]; // 输出 cherry
2. How to create a two-dimensional array?
To create an array, we use the array() function. And in order to create a two-dimensional array, we need to set the array in an array element. The following is an example of a simple two-dimensional array:
$cars = array( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) );
This array has four elements, and each element is an array of three elements. The first element of the subarray represents the brand, the second element represents the inventory quantity, and the third element represents the quantity sold.
3. How to get the value of a two-dimensional array?
In PHP, there are two ways to access the values of a two-dimensional array: by position or by name.
- Access by position
To access a value in a two-dimensional array, you need to provide the subscript of the element (that is, at which position). In the $cars 2D array above, the first array element has index 0, the second has index 1, and so on. The following is the code to access the array by position:
echo $cars[0][0].": 库存: ".$cars[0][1].", 销售: ".$cars[0][2].".<br>"; echo $cars[1][0].": 库存: ".$cars[1][1].", 销售: ".$cars[1][2].".<br>"; echo $cars[2][0].": 库存: ".$cars[2][1].", 销售: ".$cars[2][2].".<br>"; echo $cars[3][0].": 库存: ".$cars[3][1].", 销售: ".$cars[3][2].".<br>";
The output of the above code is as follows:
Volvo: 库存: 22, 销售: 18. BMW: 库存: 15, 销售: 13. Saab: 库存: 5, 销售: 2. Land Rover: 库存: 17, 销售: 15.
- Access by name
Similar to a one-dimensional array , you can also access the values of a two-dimensional array by using the name of the associative array. In a two-dimensional array, to access a value, we need to provide two keys: the first key is used to select the array element, and the second key is used to select the element in the inner array. Here is the code to access the $cars array by name:
echo $cars[0]["0"].": 库存: ".$cars[0]["1"].", 销售: ".$cars[0]["2"].".<br>"; echo $cars[1]["0"].": 库存: ".$cars[1]["1"].", 销售: ".$cars[1]["2"].".<br>"; echo $cars[2]["0"].": 库存: ".$cars[2]["1"].", 销售: ".$cars[2]["2"].".<br>"; echo $cars[3]["0"].": 库存: ".$cars[3]["1"].", 销售: ".$cars[3]["2"].".<br>";
Note that in a two-dimensional array, we need to specify both keys to access the elements.
The above code output results are the same as access by location.
4. Summary
Two-dimensional array is one of the commonly used types in PHP. Creating a two-dimensional array is very simple. You only need to put an array in the array element. When accessing elements of a two-dimensional array, you can access them by position or name. If you need to traverse the entire two-dimensional array, you can use a double for loop to complete it.
The above is the detailed content of How to get the value of two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment