Home  >  Article  >  Backend Development  >  How to convert php indexed two-dimensional array to one-dimensional

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

WBOY
WBOYOriginal
2023-05-11 10:28:06414browse

In PHP programming, arrays are usually important tools used to store and process data. In some cases, we need to convert a two-dimensional array into a one-dimensional array to facilitate subsequent processing and use. This article will introduce how to use the PHP programming language to implement this function.

First, we need to define a two-dimensional array as an example. The following is a simple example:

$cars = array(
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
);

The above array represents four cars, including car name, sales volume and inventory. Next, we will show several methods to convert a two-dimensional array into a one-dimensional array.

  1. Using a foreach loop

We can use a foreach loop to traverse each element of a two-dimensional array and convert it into an element in a one-dimensional array. The following is the code implementation:

$flat_cars = array();
foreach($cars as $car) {
  foreach($car as $detail) {
    $flat_cars[] = $detail;
  }
}

In the above code, an empty array $flat_cars will be created. Through the foreach loop, each element $car in the two-dimensional array $cars is traversed and decomposed into a one-dimensional array $detail. Then, add the elements in $detail to the $flat_cars array. Finally, $flat_cars becomes a one-dimensional array, where each element corresponds to an element in the original two-dimensional array $cars.

  1. Using the array_reduce function

PHP's array_reduce function can accumulate an array through the specified callback function and return the last accumulated result. We can use this function to convert a two-dimensional array into a one-dimensional array.

$flat_cars = array_reduce($cars, 'array_merge', array());

In the above code, first we call the array_reduce function, pass in the original array $cars, and specify a callback function 'array_merge' for cumulative calculation. The 'array_merge' function will be incorporated as a parameter into the array_reduce function. Finally, by specifying an empty array as the third parameter of the array_reduce function, the $cars array can be converted into a one-dimensional array $flat_cars.

  1. Use the array_column function

The array_column function is a very practical function after PHP 5.5. It can extract the value of a specific column in the array and return a one-dimensional array. . We can use it to convert a two-dimensional array into a one-dimensional array.

$flat_cars = array_column($cars, null, 0);

In the above code, we call the array_column function, passing in the original array $cars and the second parameter is null. The first parameter $cars indicates that the array of specific columns needs to be extracted, while the second parameter null indicates that the entire row of data will be extracted. The last parameter 0 means that the first element of the specified column (i.e. car name) will be extracted and a one-dimensional array will be returned. This one-dimensional array contains all elements of the $cars two-dimensional array.

Summary

In PHP programming, converting a two-dimensional array into a one-dimensional array is an extremely common operation. In this article, we introduce three methods to achieve this function, using foreach loop, array_reduce function and array_column function. Each method has its applicable scenarios, advantages and disadvantages, and you need to choose according to the actual situation when using it.

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