In PHP, processing arrays is one of the most common requirements. Sometimes we need to convert a one-dimensional array into a two-dimensional array. This process may involve a variety of data operations, which requires us to flexibly use array functions to achieve the conversion. This article will introduce some methods and techniques for converting one-dimensional arrays into two-dimensional arrays in PHP.
1. The need to convert a one-dimensional array into a two-dimensional array
In PHP, one-dimensional arrays and two-dimensional arrays are common data structures. A one-dimensional array is an array with only one dimension and is usually used to store a set of related data, such as a student's test score. A two-dimensional array contains an array of two dimensions and is usually used to store multiple sets of related data, such as the test scores of all students in a class. In PHP, we usually use the array function to create one-dimensional arrays, and the second-level array function to create two-dimensional arrays. Here is a simple example for creating a one-dimensional array and a two-dimensional array:
$array1 = array("语文"=>85, "数学"=>90, "英语"=>88); $array2 = array( array("姓名"=>"张三","语文"=>85,"数学"=>90,"英语"=>88), array("姓名"=>"李四","语文"=>89,"数学"=>92,"英语"=>86), array("姓名"=>"王五","语文"=>90,"数学"=>87,"英语"=>91), array("姓名"=>"赵六","语文"=>88,"数学"=>91,"英语"=>89) );
Sometimes we encounter situations where we need to convert a one-dimensional array into a two-dimensional array. For example, we have a group of students' test scores. We need to group this group of scores according to certain rules, and then convert them into a two-dimensional array to facilitate subsequent processing or display. This is the need to convert a one-dimensional array into a two-dimensional array. Some implementation methods will be introduced below.
2. Use the array_chunk function
The array_chunk function is one of PHP's built-in functions. Its function is to divide a one-dimensional array into multiple small arrays of specified lengths, and then combine these small arrays into A new two-dimensional array is returned. For example, we have the following one-dimensional array:
$students_scores = array(85, 90, 88, 89, 92, 86, 90, 87, 91, 88, 91, 89);
We need to group this array into groups of 4 and then convert it into a new two-dimensional array. You can use the following method:
$grouped_scores = array_chunk($students_scores, 4);
This method will return a two-dimensional array of length 3, each small array contains 4 elements. If the length of the original array is not an integer multiple of the length of the small array, the last small array will contain the remaining elements. We can use the var_dump function to view the results:
var_dump($grouped_scores);
The running results are as follows:
array (size=3) 0 => array (size=4) 0 => int 85 1 => int 90 2 => int 88 3 => int 89 1 => array (size=4) 0 => int 92 1 => int 86 2 => int 90 3 => int 87 2 => array (size=4) 0 => int 91 1 => int 88 2 => int 91 3 => int 89
As you can see, the original array is divided into three small arrays of length 4, and then these small arrays A new two-dimensional array is formed.
3. Use the array_map function
The array_map function is one of PHP's built-in functions. Its function is to apply the specified callback function to each element of one or more arrays. We can use the array_map function to convert a one-dimensional array into a two-dimensional array. The specific method is as follows:
function group_by($n, $array) { return array_chunk($array, $n); } $students_scores = array(85, 90, 88, 89, 92, 86, 90, 87, 91, 88, 91, 89); $grouped_scores = array_map('group_by', array(4), array($students_scores));
Among them, the group_by function is used to group a one-dimensional array into a two-dimensional array of length $n$. We then apply this function to the $students_scores$ array using the array_map function, resulting in a new 2D array. If the lengths to be divided are different, you can change the first parameter of the array function to an array with a length of $m$, where $m$ is the length to be divided. For example, the following code splits the original array into a two-dimensional array of length 3 and a length of 4:
$grouped_scores = array_map('group_by', array(3,4), array($students_scores));
4. Use loop traversal
In PHP, we can use loops The traversal method converts a one-dimensional array into a two-dimensional array. The specific method is as follows:
$students_scores = array(85, 90, 88, 89, 92, 86, 90, 87, 91, 88, 91, 89); $grouped_scores = array(); for ($i = 0; $i < count($students_scores); $i += 4) { $grouped_scores[] = array_slice($students_scores, $i, 4); }
Among them, the array_slice function is used to obtain the specified part of an array. Here, we use loop traversal to split the original array, taking out 4 elements starting from $i$ each time, and then adding them to the result array as a small array.
5. Summary
There are many ways to convert a one-dimensional array to a two-dimensional array in PHP. The most common is to use the array_chunk function or loop traversal. The array_map function is also a more flexible method and can use different split lengths according to different needs. In practical applications, we need to choose different methods according to the actual situation to achieve the best conversion effect.
The above is the detailed content of Convert one-dimensional array to two-dimensional array php. For more information, please follow other related articles on the PHP Chinese website!