The two-dimensional array in PHP is a very common and important data type. It has the characteristics of a multi-dimensional isomorphic array and is also one of the knowledge points that must be mastered in the process of learning PHP. In actual development, we often encounter scenarios where we need to use two-dimensional arrays, such as processing forms, operating databases, etc. This article will introduce in detail the definition, assignment, access and some practical operation skills of PHP two-dimensional array.
What is a two-dimensional array in PHP?
In PHP, a two-dimensional array is an array composed of multiple one-dimensional arrays. Each one-dimensional array can contain multiple units, and each unit is called an element. These one-dimensional arrays are identified and accessed through indexes or key-value pairs. Therefore, two-dimensional arrays are suitable for multi-dimensional data structures and can better describe and store information about multiple related data.
Definition and assignment of two-dimensional arrays
The method of defining and assigning two-dimensional arrays is similar to one-dimensional arrays, and can be created using the array() keyword or []. However, unlike a one-dimensional array, each element of a two-dimensional array is a one-dimensional array, so you need to add [] or array() inside the one-dimensional array again for definition and initialization.
The following are some examples:
// 定义关联型二维数组 $users = array( array('id' => 1, 'name' => 'Tom', 'age' => 20), array('id' => 2, 'name' => 'Jerry', 'age' => 22), array('id' => 3, 'name' => 'Mike', 'age' => 24) ); // 定义索引型二维数组 $students = [ ['Tom', 'Mike', 'Jerry'], ['20', '24', '22'] ]; // 定义空二维数组 $emptyArr = [[]];
In actual development, we can choose different types of two-dimensional arrays to define and initialize according to different needs. For example, associative two-dimensional arrays are suitable for storing structured data, while indexed two-dimensional arrays are suitable for storing unstructured data and are more convenient to access.
Access to two-dimensional arrays
The method of accessing two-dimensional arrays in PHP is also very simple. You can use [] or {} to select the required elements. When using [], you need to enter the row index or key value in front and the column index or key value in the back. It should be noted that if you use key-value pairs to store data, they must be initialized when defined.
The following are some examples:
// 访问关联型二维数组 echo $users[0]['id']; // 输出 1 echo $users[1]['name']; // 输出 Jerry // 访问索引型二维数组 echo $students[1][0]; // 输出 20 echo $students[0][2]; // 输出 Jerry
In addition, we can also use foreach loop to traverse the two-dimensional array, and nested loops can access and operate each element.
// 使用foreach循环遍历关联型二维数组 foreach($users as $user) { echo 'id: '.$user['id'].', name: '.$user['name'].', age: '.$user['age'].'<br>'; } // 使用for循环遍历索引型二维数组 for($i=0; $i<count($students); $i++) { for($j=0; $j<count($students[$i]); $j++) { echo $students[$i][$j].'<br>'; } }
Common operation skills for two-dimensional arrays
In addition to the above basic operations, PHP also provides some common two-dimensional array operation skills, which allow us to operate and process data more conveniently .
In actual development, we usually need to sort two-dimensional arrays. We can use sort(), rsort(), asort(), arsort () and other functions sort the array in ascending or descending order according to the value or key name of the elements. It should be noted that before sorting, you need to ensure that each element is a one-dimensional array, otherwise errors or exceptions will occur.
// 将关联型二维数组按照id升序排列 array_multisort(array_column($users,'id'),SORT_ASC,$users); print_r($users);
In addition, we can also use the array_filter() function to filter the two-dimensional array and return a new array composed of elements that meet the conditions. This function can customize a condition, filter out elements that do not meet the condition and return the newly generated array.
// 过滤关联型二维数组年龄大于20的元素 $filteredUsers = array_filter($users, function($user) { return $user['age']>20; }); print_r($filteredUsers);
Use the array_merge() function to merge multiple arrays into one array. For two-dimensional arrays, you need to ensure that each element is a one-dimensional array, otherwise errors or exceptions will occur.
// 合并两个关联型二维数组 $newUsers = array_merge($users, array( array('id' => 4, 'name' => 'Lucy', 'age' => 26), array('id' => 5, 'name' => 'John', 'age' => 28) )); print_r($newUsers);
Summary
The above are the basic concepts, definition, assignment, access and some practical operation skills of PHP two-dimensional array. In actual development, we need to master these knowledge points and flexibly use two-dimensional arrays to perform programming work more efficiently. At the same time, we also need to pay attention to factors such as the dimension, type, and size of the array to ensure that the program can run normally and efficiently and improve the maintainability and readability of the program.
The above is the detailed content of How to write a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!