The conversion of multi-dimensional arrays and one-dimensional arrays in PHP is not a difficult operation. This goal can be easily achieved by using some functions correctly. This article introduces several implementation methods.
In PHP, arrays are a very useful and commonly used data type, and are often used in actual development. In addition to providing regular operations for storing and accessing elements, PHP arrays also allow the use of multi-dimensional arrays and one-dimensional arrays, which are used to store complex structural data and some simple data respectively.
Multidimensional array means that other arrays are nested inside the array, that is, the array elements are also an array. They can have any dimensions, but each array must have a logically unique key. For example, a multi-dimensional array used to store the grades of three courses can be represented as:
$score = array( "math" => array( "Tom" => 89, "Jerry" => 92, "John" => 76 ), "english" => array( "Tom" => 78, "Jerry" => 85, "John" => 91 ), "science" => array( "Tom" => 95, "Jerry" => 84, "John" => 88 ) );
while a one-dimensional array refers to an array in which each element contains only one value, and this value can be one character. Strings, numbers, Boolean values, etc. The keys of a one-dimensional array can be strings or numbers, but the values of numeric keys must be integers or floating point numbers. For example:
$fruit = array("apple", "orange", "banana", "pear");
Converting multi-dimensional array to one-dimensional array is a very common operation. For some operations that require sorting, comparison, and search of elements of multi-dimensional arrays, etc. Generally speaking, the use of one-dimensional arrays will be more convenient. The following introduces two methods of converting multi-dimensional arrays to one-dimensional arrays.
Recursion is a very powerful method that allows us to easily handle many tasks, including processing complex multi-dimensional arrays. By using a recursive function to convert a multidimensional array into a one-dimensional array, you can gradually reduce the number of levels of the array and eventually move all the elements into a new one-dimensional array. The specific implementation is as follows:
function multi_to_one($multi_arr) { static $result_arr = array(); foreach ($multi_arr as $key => $val) { if (is_array($val)) { multi_to_one($val); } else { $result_arr[$key] = $val; } } return $result_arr; }
In this function, we traverse each element. If we find that the current element is an array, then we perform a recursive operation on it until the element is no longer an array; otherwise we Adds the current element to the static array $result_arr
defined in the function. Finally, we return the processed one-dimensional array.
Use this function to convert the multidimensional array $score
above into a one-dimensional array:
$result = multi_to_one($score); print_r($result);
The output result is:
Array ( [Tom] => 95 [Jerry] => 84 [John] => 88 )
There is a function in PHP specifically for traversing arraysarray_walk_recursive
, which can traverse every element in a multi-dimensional array. This function can accept a callback function as second argument, in which we can manipulate the element and add it to a new one-dimensional array. The following is the specific implementation:
function flatten_array($multi_arr) { $result_arr = array(); array_walk_recursive($multi_arr, function($val, $key) use (&$result_arr) { $result_arr[$key] = $val; }); return $result_arr; }
Compared with the first method, this method uses an anonymous function to complete the callback operation. This function uses the use
keyword to set the external variable$result_arr
is introduced and stores the processed elements in this array. Finally, the generated one-dimensional array is returned.
Using this function, you can also convert the above multi-dimensional array $score
into a one-dimensional array:
$result = flatten_array($score); print_r($result);
The output result is:
Array ( [Tom] => 95 [Jerry] => 84 [John] => 88 )
Converting a one-dimensional array to a multi-dimensional array is also a very common requirement. In practical applications, it is often encountered that one-dimensional arrays need to be grouped according to certain conditions. The following describes a method to convert a one-dimensional array into a multi-dimensional array.
array_reduce
is a higher-order function in PHP, which can traverse the array like array_walk_recursive
, but with array_walk_recursive
The difference is that array_reduce
can also accept a parameter as the initial value of the function, and this value will become the initial value of each callback function and passed to them. array_reduce
can be used for various types of calculations and processing operations, and we can use it to convert one-dimensional arrays into multi-dimensional arrays.
The following is the specific implementation:
function group_array($data_arr, $group_key) { $result_arr = array_reduce($data_arr, function(&$result, $item) use ($group_key) { $index = $item[$group_key]; if (!isset($result[$index])) { $result[$index] = array(); } $result[$index][] = $item; return $result; }, array()); return $result_arr; }
In this function, we use an anonymous function to execute the callback function. When traversing the array, if the $group_key
of the current element Before the values have been added to the new multidimensional array, we create a new array element as its key, and then add the current element to the value of this new array. During the entire process, the first parameter $result
of the callback function will be continuously modified and passed until a processed multi-dimensional selection array is finally returned.
Use this function to convert a one-dimensional array:
$data = array( array("id" => 1, "name" => "Tom", "group" => "A"), array("id" => 2, "name" => "Jerry", "group" => "A"), array("id" => 3, "name" => "John", "group" => "B"), array("id" => 4, "name" => "Smith", "group" => "C"), );
according to the "group" key into a multi-dimensional array:
$result = group_array($data, "group"); print_r($result);
The output result is:
Array ( [A] => Array ( [0] => Array ( [id] => 1 [name] => Tom [group] => A ) [1] => Array ( [id] => 2 [name] => Jerry [group] => A ) ) [B] => Array ( [0] => Array ( [id] => 3 [name] => John [group] => B ) ) [C] => Array ( [0] => Array ( [id] => 4 [name] => Smith [group] => C ) ) )
The conversion of multi-dimensional arrays and one-dimensional arrays in PHP is a requirement we often encounter in daily development. We can use recursive functions or higher-order functions to convert multi-dimensional arrays into one-dimensional arrays or convert one-dimensional arrays into multi-dimensional arrays. In practical applications, we need to choose different methods according to different situations and use appropriate functions to complete the conversion.
The above is the detailed content of How to convert multidimensional array to one-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!