Home > Article > Backend Development > Convert between multidimensional arrays in php
In PHP programming, arrays are a very commonly used data structure, and multi-dimensional arrays can make it more convenient for us to process complex data. However, sometimes we need to convert multi-dimensional arrays into other forms of arrays, or convert other forms of arrays into multi-dimensional arrays. This article will introduce how to convert between multidimensional arrays in PHP.
1. Convert a multi-dimensional array to a one-dimensional array
In some cases, we need to convert a multi-dimensional array into a one-dimensional array to facilitate data storage or transmission. PHP provides a very convenient function array_values(), which can convert a multi-dimensional array into a one-dimensional array. Look at the following sample code:
$multi_dimension_array = array( array('a', 'b', 'c'), array('d' => 'e', 'f' => 'g'), array(1, 2, 3) ); $one_dimension_array = array_values(array_reduce($multi_dimension_array, 'array_merge', array())); print_r($one_dimension_array);
The output result is:
Array ( [0] => a [1] => b [2] => c [3] => e [f] => g [4] => 1 [5] => 2 [6] => 3 )
In the above sample code, we first define a multi-dimensional array $multi_dimension_array, which contains three sub-arrays. We then used PHP's array_reduce() function and array_merge() function to merge multiple subarrays into a one-dimensional array. Finally, we use the array_values() function to re-index the merged one-dimensional array and output the result.
2. Convert a one-dimensional array to a multi-dimensional array
Similarly, sometimes we also need to convert a one-dimensional array to a multi-dimensional array. PHP also provides some practical functions to achieve this purpose. Two methods will be introduced below.
The array_chunk() function in PHP can split a one-dimensional array into multiple small arrays according to the specified size and return a New two-dimensional array. The sample code is as follows:
$one_dimension_array = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'); $multi_dimension_array = array_chunk($one_dimension_array, 3); print_r($multi_dimension_array);
The output result is:
Array ( [0] => Array ( [0] => a [1] => b [2] => c ) [1] => Array ( [0] => d [1] => e [2] => f ) [2] => Array ( [0] => g [1] => h [2] => i ) [3] => Array ( [0] => j ) )
In the above sample code, we first define a one-dimensional array $one_dimension_array. Then, we use the array_chunk() function to split $one_dimension_array into multiple small arrays of length 3 and return a new two-dimensional array $multi_dimension_array.
If you don’t want to use the array_chunk() function, you can also use foreach loop to convert a one-dimensional array into a multi-dimensional array. The sample code is as follows:
$one_dimension_array = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'); $multi_dimension_array = array(); $count = 0; foreach ($one_dimension_array as $item) { $multi_dimension_array[floor($count / 3)][$count % 3] = $item; $count++; } print_r($multi_dimension_array);
The output result is:
Array ( [0] => Array ( [0] => a [1] => b [2] => c ) [1] => Array ( [0] => d [1] => e [2] => f ) [2] => Array ( [0] => g [1] => h [2] => i ) [3] => Array ( [0] => j ) )
In the above sample code, we first define an empty two-dimensional array $multi_dimension_array, and a count variable $count. We then use a foreach loop to iterate through each element in the $one_dimension_array array and add the element to the $multi_dimension_array array. We use floor($count / 3) to determine the index of the new subarray, and $count % 3 to determine the index of the new element in the subarray. Finally, we use the print_r() function to output the results.
3. Summary
In PHP programming, processing arrays is a very common task. Whether it is converting a multi-dimensional array to a one-dimensional array, or converting a one-dimensional array to a multi-dimensional array, PHP provides some very practical functions and methods. Through the introduction of this article, I believe that readers have understood the usage of these functions and methods and can flexibly use them in actual development.
The above is the detailed content of Convert between multidimensional arrays in php. For more information, please follow other related articles on the PHP Chinese website!