In PHP, multidimensional array is a very common data structure. Four-dimensional array is one of the types, which can be regarded as a three-dimensional array nested one layer further. When dealing with four-dimensional arrays, sometimes we need to retrieve a specific value from it. Let's explore how to retrieve values from a four-dimensional array.
First of all, we need to understand the basic structure of a four-dimensional array. A four-dimensional array can be viewed as an array composed of multiple three-dimensional arrays. Each three-dimensional array can be regarded as an array composed of multiple two-dimensional arrays, and so on. The last array is an array composed of multiple single elements.
Next, we give an example to illustrate how to get values from a four-dimensional array.
Suppose we have a four-dimensional array $arr, which stores information at four levels of school, class, student and grade, as shown below:
$arr = [ "school1" => [ "class1" => [ "stu1" => ["score" => 85], "stu2" => ["score" => 78], "stu3" => ["score" => 92], ], "class2" => [ "stu1" => ["score" => 88], "stu2" => ["score" => 90], "stu3" => ["score" => 82], ], ], "school2" => [ "class1" => [ "stu1" => ["score" => 95], "stu2" => ["score" => 91], "stu3" => ["score" => 87], ], "class2" => [ "stu1" => ["score" => 85], "stu2" => ["score" => 90], "stu3" => ["score" => 93], ], ], ];
Now we need to take it out from $arr Results of School 1, Class 2, and Student 3. In fact, it is very simple. You only need to access it layer by layer according to the value method of multi-dimensional array. The specific code is as follows:
$score = $arr["school1"]["class2"]["stu3"]["score"]; echo $score; // 输出:82
The above is the basic method of getting values from PHP four-dimensional array. It should be noted that when obtaining the value, make sure that the corresponding key names at each level exist, otherwise errors such as "Undefined index" will occur. In order to avoid this situation, it is recommended to use isset() or array_key_exists() function to judge before getting the value.
In addition, we can also use for loop or foreach loop to traverse and take out all the values in the four-dimensional array. The code is as follows:
// 使用 for 循环遍历四维数组 for ($i = 0; $i < count($arr); $i++) { $schools = $arr[$i]; foreach ($schools as $classes) { foreach ($classes as $students) { foreach ($students as $scores) { echo $scores["score"] . "<br>"; } } } } // 使用 foreach 循环遍历四维数组 foreach ($arr as $schools) { foreach ($schools as $classes) { foreach ($classes as $students) { foreach ($students as $scores) { echo $scores["score"] . "<br>"; } } } }
The above is the basic method of obtaining values from four-dimensional arrays in PHP. It should be noted that the value operation of multi-dimensional arrays will have a certain impact on program performance, so you need to pay attention to optimization when using it.
The above is the detailed content of How to get the value of four-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!