Home >Backend Development >PHP Problem >How to get the value of two-dimensional array in php

How to get the value of two-dimensional array in php

王林
王林Original
2023-05-19 13:07:381079browse

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.

  1. 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.
  1. 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!

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