Home > Article > Backend Development > How to convert one-dimensional array to multi-dimensional array in php
PHP is a widely used server-side scripting language, especially suitable for the field of web development. In PHP, array is a very important data type that can be used to store multiple data items. One-dimensional arrays are the simplest and can be easily created, but for complex data collections, multi-dimensional arrays are more practical. In this article, I will explore how to convert a one-dimensional array into a multi-dimensional array to make the data structure clearer.
First let’s look at how to create a one-dimensional array. In PHP, you can create a one-dimensional array through the following syntax:
$numbers = array(1, 2, 3, 4, 5);
The above code creates an array named $numbers, which contains five integers. In PHP 5.4 or above, you can also create it using short array syntax:
$numbers = [1, 2, 3, 4, 5];
Next, we will introduce how to convert a one-dimensional array into a two-dimensional array or a multi-dimensional array.
1. Convert a one-dimensional array into a two-dimensional array
The method of converting a one-dimensional array into a two-dimensional array is to convert each element in the one-dimensional array as a two-dimensional array. a value of . For a one-dimensional array with n elements, we can convert it into a two-dimensional array with n elements, where each element is a one-dimensional array containing only one value. The following is an example:
$numbers = array(1, 2, 3, 4, 5); $grid = array_chunk($numbers, 1); print_r($grid);
In the above example, we use the array_chunk() function to convert the $numbers array into a two-dimensional array containing 5 arrays. The syntax of this function is as follows:
array_chunk(array, size, preserve_key);
Among them, the array parameter is the one-dimensional array to be converted; the size parameter is the number of elements of each sub-array; the preserve_key parameter specifies whether the keys of the original array should be preserved when segmenting . The size parameter here is set to 1, and each subarray contains only one element.
The following outputs the $grid array at this time:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) [4] => Array ( [0] => 5 ) )
As shown above, the $grid array has been successfully converted into a two-dimensional array.
2. Convert a one-dimensional array into a multi-dimensional array
The method of converting a one-dimensional array into a multi-dimensional array is to treat each element in the one-dimensional array as a value in the multi-dimensional array . If you need a three-dimensional array, you can follow these steps:
The following is an example of converting to a three-dimensional array:
$numbers = array(1, 2, 3, 4, 5); $grid = array_chunk($numbers, 1); $cube = array_map(function($val) { return array($val); }, $grid); print_r($cube);
In the above code, we first used the array_chunk() function to convert the $numbers array into an array containing 5 two-dimensional array. Then, use the array_map() function to convert each one-dimensional array into an array containing one element. The syntax of this function is as follows:
array_map(callback, arr1, arr2, ...);
Among them, the callback parameter is a custom function name used to operate on each array element; the arr1, arr2,... parameters are each array to be operated on .
Note: If you use an anonymous function, you need to add the use ($variable1, ...) statement before the function to pass the variables that need to be used in the closure function.
The following outputs the $cube array at this time:
Array ( [0] => Array ( [0] => Array ( [0] => 1 ) ) [1] => Array ( [0] => Array ( [0] => 2 ) ) [2] => Array ( [0] => Array ( [0] => 3 ) ) [3] => Array ( [0] => Array ( [0] => 4 ) ) [4] => Array ( [0] => Array ( [0] => 5 ) ) )
As shown above, the $cube array has been successfully converted into a three-dimensional array.
3. Summary
In PHP, array is a very important data type. One-dimensional arrays can be easily created, but for complex data collections, multi-dimensional arrays are more practical. By converting a one-dimensional array into a two-dimensional array or a multi-dimensional array, the data structure can be made clearer and facilitate data search and processing. Through the introduction of this article, I believe that readers have a deeper understanding of how to convert one-dimensional arrays into multi-dimensional arrays.
The above is the detailed content of How to convert one-dimensional array to multi-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!