In PHP, we often need to use arrays to store and manage data. Sometimes, we encounter an array that contains another array, which is a so-called multi-dimensional array. How to call an array contained within another array in PHP? This article will introduce this in detail.
A multidimensional array refers to an array that stores another or multiple arrays in an array. They can have multiple nesting levels, or they can be of different data types. For example:
$arr = array( '中国' => array('北京', '上海', '广州'), '美国' => array('纽约', '洛杉矶', '芝加哥'), '英国' => array('伦敦', '曼彻斯特', '爱丁堡') );
In this example, the $arr array contains 3 key-value pairs, each key-value pair is an array, and these arrays contain several city names. This is a two-dimensional array because its nesting level is 2.
Of course, multi-dimensional arrays can be more complex, and the number of nested levels can be increased at will.
In PHP, to access an element in a multi-dimensional array, you need to use a subscript (that is, a key value). access. For example, we can use the following method to print out the first city in the Chinese city in the array $arr:
echo $arr['中国'][0]; //输出北京
Note here that $arr['China'] returns an array, not Specific city name, so we can then use square brackets [] to access the elements in this array. Similarly, if you want to visit the third city in the United States (i.e. Chicago), you can write like this:
echo $arr['美国'][2]; //输出芝加哥
The access mode of multi-dimensional arrays is similar to that of one-dimensional arrays, except that you need to add an extra bracket to access its nested subarray. If you need to modify the value of an element, you can also use this method to operate.
Traversing multi-dimensional arrays is something we often need to do in actual development. It's easy to just print out a certain element in a multidimensional array, but when you need to loop through the entire array, you need to use traversal.
PHP provides a variety of ways to traverse arrays, including for loops, foreach loops, while loops, etc. Here we take the foreach loop as an example to demonstrate how to traverse the above multi-dimensional array.
First, we can use a double loop to traverse the two-dimensional array. The following code prints out all the city names in the above array $arr:
foreach ($arr as $country => $cities) { echo $country . '的城市有:'; foreach ($cities as $city) { echo $city . ', '; } echo '<br>'; }
In this example, the outer foreach loop is used to traverse each element in the main array $arr, that is, all countries and their City array. Then, the inner foreach loop is used to traverse each city array and output the corresponding city name.
For higher-dimensional arrays, we need to increase the corresponding number of loop layers. This process needs to be flexibly adjusted according to actual conditions.
After understanding how to access and traverse multi-dimensional arrays, here are some common operations in practical applications:
$arr['中国'][] = '深圳'; //添加深圳到中国城市数组的末尾
//遍历方式 function findCity($arr, $cityName) { foreach ($arr as $country => $cities) { if (in_array($cityName, $cities)) { return $country; } } return false; } //in_array()方式 if (in_array('芝加哥', $arr['美国'])) { echo '美国有芝加哥这个城市。'; }
$arr['英国'][2] = '布莱顿'; //将英国城市数组中的第三个城市改成布莱顿
unset($arr['中国'][0]); //删除中国的第一个城市北京
This article introduces the basic concepts of multidimensional arrays in PHP and demonstrates how to access an array contained in another array , and how to traverse and manipulate multidimensional arrays. Multidimensional arrays are very common in actual development, and mastering its basic operations is very important for writing efficient PHP programs. Hope this article can be helpful to readers.
The above is the detailed content of How to call a subarray in an array in php. For more information, please follow other related articles on the PHP Chinese website!