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.
- 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); ?>
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 ) )
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.
- 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); ?>
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.
- 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); ?>
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] => 男 ) ) ) )
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft