How to convert two-dimensional array to three-dimensional array in php

青灯夜游
Release: 2023-03-15 21:14:02
Original
3049 people have browsed it

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)".

How to convert two-dimensional array to three-dimensional array in php

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);
Copy after login

How to convert two-dimensional array to three-dimensional array in php

Convert the two-dimensional array to a three-dimensional array

var_dump(array_chunk($arr,1));
Copy after login

How to convert two-dimensional array to three-dimensional array in php

var_dump(array_chunk($arr,2));
Copy after login

How to convert two-dimensional array to three-dimensional array in php

var_dump(array_chunk($arr,3));
Copy after login

How to convert two-dimensional array to three-dimensional array in php

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 ] )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!