In PHP, you can use the array_chunk() function to convert a two-dimensional array into a three-dimensional array. This function can split the array and split an array into multiple array chunks. You only need to pass the second parameter. Just set the length (number of elements) of each new array block; the syntax is "array_chunk (two-dimensional array, integer value)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use array_chunk () function to convert a two-dimensional array into a three-dimensional array.
array_chunk() function splits an array into new array chunks.
Use this function to divide the array of each layer of the two-dimensional array (fractal dimension) and then convert it into a three-dimensional array.
For example: There is such a two-dimensional array:
$arr= array ( array("张三",25,"男"), array("李四",21,"男"), array("娜娜",22,"女"), array("张三",25,"男"), array("李四",21,"男"), array("娜娜",22,"女") ); echo "原二维数组:"; var_dump($arr);
Convert the two-dimensional array to a three-dimensional array
var_dump(array_chunk($arr,1));
var_dump(array_chunk($arr,2));
var_dump(array_chunk($arr,3));
It can be seen that the value of the third parameter is different, and the number of elements contained in the third layer of the three-dimensional array The numbers are different.
Description:
array_chunk() function can split an array into multiple array chunks, its syntax is as follows:
array array_chunk ( array $arr , int $size [, bool $preserve_keys = false ] )
Parameter description:
arr represents the array to be divided;
size represents the number of elements of the divided sub-array;
preserve_keys indicates whether to retain the original key names in the arr array. The default is false, that is, not retained. Each sub-array after split will use a new numeric index starting from 0; if set to true, it will be retained. The original key name in arr.
array_chunk() will split the arr array into multiple sub-arrays, and the number of elements in each sub-array is determined by size. The last subarray may have less than size elements.
Return value: Returns a multi-dimensional array composed of divided sub-arrays.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert two-dimensional array to three-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!