How to merge one-dimensional array into two-dimensional array in php

PHPz
Release: 2023-04-25 09:31:10
Original
872 people have browsed it

During the development process, we often encounter situations where we need to convert a one-dimensional array into a two-dimensional array. This problem is easily solved in PHP. This article will introduce two methods to merge one-dimensional arrays into two-dimensional arrays.

Method 1: Use the array_chunk function

The array_chunk function can split an array into multiple arrays, where the number of elements in each array can be specified. The syntax of this function is as follows:

array array_chunk(array $array, int $size, bool $preserve_keys = false)
Copy after login

Among them, $array is the array to be split, $size is the number of elements in each array, and $preserve_keys indicates whether to retain the key names of the original array.

The sample code is as follows:

$array = array('cat', 'dog', 'fish', 'bird', 'hamster', 'rabbit');
$result = array_chunk($array, 2);
print_r($result);
Copy after login

This code will output the following results:

Array
(
    [0] => Array
        (
            [0] => cat
            [1] => dog
        )

    [1] => Array
        (
            [0] => fish
            [1] => bird
        )

    [2] => Array
        (
            [0] => hamster
            [1] => rabbit
        )

)
Copy after login
Copy after login

The above code divides the original array into three arrays, with two in each array element.

Next, we can use a foreach loop to iterate through the elements in each two-dimensional array and merge them into one two-dimensional array.

The sample code is as follows:

$array = array('cat', 'dog', 'fish', 'bird', 'hamster', 'rabbit');
$result = array_chunk($array, 2);
$new_array = array();
foreach ($result as $res) {
    $new_array[] = $res;
}
print_r($new_array);
Copy after login

The above code will output the following results:

Array
(
    [0] => Array
        (
            [0] => cat
            [1] => dog
        )

    [1] => Array
        (
            [0] => fish
            [1] => bird
        )

    [2] => Array
        (
            [0] => hamster
            [1] => rabbit
        )

)
Copy after login
Copy after login

Now, we have successfully merged the one-dimensional array into a two-dimensional array.

Method 2: Use for loop

Using for loop can also merge one-dimensional arrays into two-dimensional arrays. This method is relatively straightforward.

The sample code is as follows:

$array = array('cat', 'dog', 'fish', 'bird', 'hamster', 'rabbit');
$new_array = array();
for ($i = 0; $i < count($array); $i += 2) {
    $new_array[] = array($array[$i], $array[$i + 1]);
}
print_r($new_array);
Copy after login

The above code will output the following results:

Array
(
    [0] => Array
        (
            [0] => cat
            [1] => dog
        )

    [1] => Array
        (
            [0] => fish
            [1] => bird
        )

    [2] => Array
        (
            [0] => hamster
            [1] => rabbit
        )

)
Copy after login

The above code uses a for loop to split the original one-dimensional array into multiple pieces containing two A two-dimensional array of elements.

Summary

The above are two methods of merging one-dimensional arrays into two-dimensional arrays. The array_chunk function can split an array into multiple arrays. The number of elements in each array can be specified. It is more straightforward to use a for loop. I hope this article can be helpful to readers who are learning PHP.

The above is the detailed content of How to merge one-dimensional array into two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

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!