Home  >  Article  >  Backend Development  >  How to form a new array with every three elements in php

How to form a new array with every three elements in php

PHPz
PHPzOriginal
2023-04-24 14:49:25785browse

In PHP programming, we often need to operate arrays. One of the common tasks is to split a large array into multiple small arrays according to certain rules, which can facilitate our data processing and display. In this article, we will introduce how to use PHP to form a new array every three elements of an array.

1. Use the array_chunk function

The array_chunk function is a built-in function in PHP. It can split an array into multiple sub-arrays according to the specified size and return a new array composed of these sub-arrays. array. Therefore, we can use the array_chunk function to split the original array into multiple sub-arrays for every three elements. The sample code is as follows:

<?php
$original_array = array(1,2,3,4,5,6,7,8,9,10,11,12);
$new_array = array_chunk($original_array, 3);
print_r($new_array);
?>

After running the above code, the output result is:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

    [3] => Array
        (
            [0] => 10
            [1] => 11
            [2] => 12
        )

)

It can be seen that the original array was successfully split into four sub-arrays based on every three elements, and each sub-array contains three elements.

2. Use for loop implementation

In addition to using ready-made functions, we can also manually write code to split the original array into multiple sub-arrays for every three elements. The following is an implementation method based on a for loop:

<?php
$original_array = array(1,2,3,4,5,6,7,8,9,10,11,12);
$new_array = array();
for($i = 0; $i < count($original_array); $i += 3) {
    $sub_array = array_slice($original_array, $i, 3);
    array_push($new_array, $sub_array);
}
print_r($new_array);
?>

In the above code, we use a for loop to traverse the original array, taking out three elements each time and putting them into a new array as subarrays. Among them, the array_slice function is used to take out the 3 elements starting from $i, and the array_push function is used to add the subarray to the new array.

Compared with the array_chunk function, this method is more controllable and suitable for some special scenarios. For example, if we need to add a specific element at the end of each sub-array, we can modify the steps of the for loop. It will take a long time to achieve.

3. Summary

In PHP, forming a new array with every three elements of an array can be achieved using the array_chunk function and a for loop. We can choose the appropriate method according to actual needs to process and display data more efficiently.

The above is the detailed content of How to form a new array with every three elements in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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