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

How to convert a two-dimensional array into a one-dimensional array in php

PHPz
Release: 2023-04-27 09:39:58
Original
822 people have browsed it

In PHP programming, we often encounter the need to convert a two-dimensional array into a one-dimensional array. This situation is common in both data processing and logic processing. So, how to convert a two-dimensional array into a one-dimensional array? This article will introduce you to several simple implementation methods.

Scenario Analysis

Let’s look at a simple example first. We have a two-dimensional array, for example:

$data = array(
    array('id' => 1, 'name' => '张三', 'age' => 20),
    array('id' => 2, 'name' => '李四', 'age' => 22),
    array('id' => 3, 'name' => '王五', 'age' => 25)
);
Copy after login

We need to convert this two-dimensional array into a one-dimensional array. The key name of the one-dimensional array is required to be the id of each element in the two-dimensional array, and the key value is two. The name of each element in the dimensional array. How to achieve this?

Solution

Method 1: Use loop traversal method

The simplest implementation method is to use loop traversal method. The specific implementation process is as follows:

$result = array();
foreach ($data as $item) {
    $result[$item['id']] = $item['name'];
}
Copy after login

Explain the above code. We first define an array named $result to store the converted one-dimensional array. Then use a foreach loop to traverse the two-dimensional array $data, $item represents each element of the two-dimensional array. In each traversal, we use the 'id' key of the $item array as the key of the one-dimensional array, and the 'name' key of the $item array as the key of the one-dimensional array, and store them in the $result array. .

Finally, the content in $result is:

Array
(
    [1] => 张三
    [2] => 李四
    [3] => 王五
)
Copy after login
Copy after login
Copy after login

Method 2: Using the array_map and array_column functions

Another way to achieve this is to use the PHP built-in functions array_map and array_column. The specific implementation code is as follows:

$result = array_map(function($v){return [$v['id'] => $v['name']];}, $data);
$result = array_merge(...$result);
Copy after login

The above code implements an anonymous function, which processes the elements in the two-dimensional array into a new array. The key name of the array is 'id' and the key value is 'name'. This converts a two-dimensional array into an array containing multiple one-dimensional arrays.

Next, we use the array_merge function to merge multiple one-dimensional arrays into one one-dimensional array. At this time, the new one-dimensional array not only contains the original key values, but also includes the redundant index values. We use the new "expand operator (...)" in php5.6 to remove redundant index values ​​to get the one-dimensional array we want.

Finally, the content in $result is:

Array
(
    [1] => 张三
    [2] => 李四
    [3] => 王五
)
Copy after login
Copy after login
Copy after login

Method 3: Use the array_reduce function

Using the array_reduce function can also realize the need to convert a two-dimensional array into a one-dimensional array. The implementation code is as follows:

$result = array_reduce($data, function($carry, $item){
    $carry[$item['id']] = $item['name'];
    return $carry;
}, array());
Copy after login

In the above code, we use the array_reduce function to "merge" each element in the $data array from left to right into a new single value. The initial value is an empty array.

In each iteration, we use the 'id' key of the $item array as the key of the one-dimensional array, and the 'name' key of the $item array as the key of the one-dimensional array, and Stored in the $carry array. Ultimately, the contents of $carry are the one-dimensional array we want.

Finally, the content in $result is:

Array
(
    [1] => 张三
    [2] => 李四
    [3] => 王五
)
Copy after login
Copy after login
Copy after login

Application scenario

Converting a two-dimensional array to a one-dimensional array is common in data processing scenarios. For example, the result set we query from the database may be a two-dimensional array, and we only need a certain column in it as a one-dimensional array. In this case, we need to convert the two-dimensional array into a one-dimensional array.

At the same time, in logical processing, sometimes it is necessary to simplify a complex two-dimensional array into a one-dimensional array to facilitate data screening and processing.

Summary

This article introduces three ways to convert a two-dimensional array into a one-dimensional array: using loop traversal, using the array_map and array_column functions, and using the array_reduce function. Each method has its own implementation methods and advantages, and can be selected and used according to different scenarios and needs. Understanding and mastering these methods will help you write efficient, concise, and elegant PHP programs.

The above is the detailed content of How to convert a two-dimensional array into a 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