Home > Backend Development > PHP Problem > How to convert one-dimensional array in php

How to convert one-dimensional array in php

王林
Release: 2023-05-19 18:40:38
Original
453 people have browsed it

Convert to two-dimensional array?

In PHP programming, sometimes it is necessary to convert a one-dimensional array into a two-dimensional array, so that data processing and display can be more convenient. For example, group the data in a one-dimensional array according to certain rules and put it into a two-dimensional array to facilitate statistics and analysis. This article will introduce how to convert a one-dimensional array into a two-dimensional array in PHP, and how to group and sort data.

  1. Use the array_chunk() function for conversion

PHP provides a function called array_chunk(), which can divide an array into multiple small arrays, each small The array contains the specified number of elements. Among them, the parameter $size represents the number of elements contained in each small array, and the parameter $preserve_keys specifies whether to retain the key names of the original array. Use this function to convert a one-dimensional array into a two-dimensional array. The following is a sample code:

<?php
$original_array = array('a', 'b', 'c', 'd', 'e', 'f');
$chunked_array = array_chunk($original_array, 2);  // 将原数组按每2个元素分为一个小数组
print_r($chunked_array);
?>
Copy after login

The output of the above code is as follows:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
            [1] => f
        )

)
Copy after login

Through the array_chunk() function, we divide the original array into three small arrays, each small array contains two elements. This converts a one-dimensional array into a two-dimensional array.

  1. Use for loop for conversion

In addition to using the array_chunk() function, we can also use for loop to complete this conversion process. The following is a sample code:

<?php
$original_array = array('a', 'b', 'c', 'd', 'e', 'f');
$chunked_array = array();
$chunk_size = 2;  // 每个小数组包含元素数量
$chunk_count = ceil(count($original_array) / $chunk_size);  // 小数组数量
for ($i = 0; $i < $chunk_count; $i++) {
    $chunked_array[$i] = array_slice($original_array, $i * $chunk_size, $chunk_size);
}
print_r($chunked_array);
?>
Copy after login

The output of the above code is the same as the result of using the array_chunk() function above. In this example, we first count the number of small arrays, and then use the for loop and array_slice() function to split the original array and store it in the $chunked_array array.

  1. Group and sort data

After converting the one-dimensional array into a two-dimensional array, we can group and sort the data according to our own needs. The following is a code example:

<?php
$original_array = array(
    array('name' => '张三', 'age' => 18, 'gender' => '男'),
    array('name' => '李四', 'age' => 21, 'gender' => '女'),
    array('name' => '王五', 'age' => 22, 'gender' => '男'),
    array('name' => '赵六', 'age' => 19, 'gender' => '女')
);
$chunked_array = array_chunk($original_array, 2);  // 将原数组按每2个元素分为一个小数组
$sorted_array = array();
foreach ($chunked_array as $chunk) {
    $sorted_chunk = array();
    foreach ($chunk as $item) {
        $sorted_chunk[(int)$item['age']][] = $item;
    }
    ksort($sorted_chunk);
    $sorted_array[] = $sorted_chunk;
}
print_r($sorted_array);
?>
Copy after login

The above code defines a one-dimensional array $original_array containing four elements. Each element is an associative array containing three key-value pairs: name, age and gender. We divide the original array into a small array according to every two elements, and sort the elements in the small array according to age. Finally, we get a two-dimensional array $sorted_array, whose structure is as follows:

Array
(
    [0] => Array
        (
            [18] => Array
                (
                    [0] => Array
                        (
                            [name] => 张三
                            [age] => 18
                            [gender] => 男
                        )

                )

            [19] => Array
                (
                    [0] => Array
                        (
                            [name] => 赵六
                            [age] => 19
                            [gender] => 女
                        )

                )

        )

    [1] => Array
        (
            [21] => Array
                (
                    [0] => Array
                        (
                            [name] => 李四
                            [age] => 21
                            [gender] => 女
                        )

                )

            [22] => Array
                (
                    [0] => Array
                        (
                            [name] => 王五
                            [age] => 22
                            [gender] => 男
                        )

                )

        )

)
Copy after login

Pass In the above code, we sort the elements in the same group by age and put the final result into the $sorted_array array.

Summary

This article introduces the method of converting a one-dimensional array to a two-dimensional array in PHP, including using the array_chunk() function and for loop for conversion, and further introduces how to group data and sort. In the actual programming process, we can choose different methods for data processing and display according to our own needs.

The above is the detailed content of How to convert one-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