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

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

PHPz
Release: 2023-04-20 11:21:41
Original
1610 people have browsed it

PHP is a very popular programming language that has powerful functions for processing arrays. In PHP, two-dimensional array is a very common data type, which consists of multiple one-dimensional arrays. When we need to convert a two-dimensional array into a one-dimensional array, we need to use some specific functions and techniques. Below, we will introduce several commonly used methods.

Method 1: Use a foreach loop

The first method is to use a foreach loop to traverse the two-dimensional array and add each element to a new one-dimensional array. The following is a sample code:

$two_dim_array = array(
  array('apple', 'banana', 'cherry'),
  array('dog', 'cat', 'rat'),
  array('red', 'green', 'blue')
);

$one_dim_array = array();
foreach ($two_dim_array as $key => $value) {
  foreach ($value as $k => $v) {
    array_push($one_dim_array, $v);
  }
}
print_r($one_dim_array);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => dog
    [4] => cat
    [5] => rat
    [6] => red
    [7] => green
    [8] => blue
)
Copy after login
Copy after login
Copy after login

Method 2: Use the array_merge() function

The second method is to use the array_merge() function to merge multiple Groups of dimensions are combined into a new one-dimensional array. We can get a new one-dimensional array by looping through the two-dimensional array and merging each one-dimensional array using the array_merge() function. The following is a sample code:

$two_dim_array = array(
  array('apple', 'banana', 'cherry'),
  array('dog', 'cat', 'rat'),
  array('red', 'green', 'blue')
);

$one_dim_array = array();
foreach ($two_dim_array as $key => $value) {
  $one_dim_array = array_merge($one_dim_array, $value);
}
print_r($one_dim_array);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => dog
    [4] => cat
    [5] => rat
    [6] => red
    [7] => green
    [8] => blue
)
Copy after login
Copy after login
Copy after login

Method 3: Use the array_reduce() function

The third method is to use the array_reduce() function to convert the two-dimensional array into Convert to a one-dimensional array. The array_reduce() function accepts an array and a callback function as parameters. The callback function iterates and calculates the values ​​​​in the array one by one, and finally returns a single value. We can use an empty array as the initial value and merge each one-dimensional array into a new one-dimensional array in the callback function. The following is a sample code:

$two_dim_array = array(
  array('apple', 'banana', 'cherry'),
  array('dog', 'cat', 'rat'),
  array('red', 'green', 'blue')
);

$one_dim_array = array_reduce($two_dim_array, function($carry, $item) {
  return array_merge($carry, $item);
}, array());
print_r($one_dim_array);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => dog
    [4] => cat
    [5] => rat
    [6] => red
    [7] => green
    [8] => blue
)
Copy after login
Copy after login
Copy after login

Summary

The above are the three common methods in PHP to convert a two-dimensional array into a one-dimensional array. Using the foreach loop and the array_merge() function both require traversing the array, while using the array_reduce() function can be more concise, but it may confuse some developers. Therefore, in actual development, we can choose the most suitable method according to our needs to improve development efficiency.

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