Home  >  Article  >  Backend Development  >  How to divide 2D array into 1D array php

How to divide 2D array into 1D array php

PHPz
PHPzOriginal
2023-05-19 10:19:07565browse

In PHP, a two-dimensional array is a common data structure. It consists of multiple one-dimensional arrays, and each one-dimensional array can contain multiple elements. In some cases, we need to split a two-dimensional array into multiple one-dimensional arrays for more flexible operations and processing. This article will explain how to divide a two-dimensional array into a one-dimensional array.

1. Use foreach loop

In PHP, we can traverse a two-dimensional array by using a foreach loop, and then obtain the corresponding one-dimensional array through the array index. The following is an example of using a foreach loop to split a two-dimensional array into one-dimensional array:

$twoDimensionalArray = array(
    array('A', 'B', 'C'),
    array('D', 'E', 'F'),
    array('G', 'H', 'I')
);

$oneDimensionalArray1 = array();
$oneDimensionalArray2 = array();
$oneDimensionalArray3 = array();

foreach ($twoDimensionalArray as $key => $value) {
    $oneDimensionalArray1[] = $value[0];
    $oneDimensionalArray2[] = $value[1];
    $oneDimensionalArray3[] = $value[2];
}

print_r($oneDimensionalArray1);
print_r($oneDimensionalArray2);
print_r($oneDimensionalArray3);

In this example, we use a foreach loop to iterate over the two-dimensional array $twoDimensionalArray and get each one-dimensional array by array index Elements. Then, we divide the two-dimensional array into three one-dimensional arrays $oneDimensionalArray1, $oneDimensionalArray2, and $oneDimensionalArray3 by adding the element values ​​to the corresponding one-dimensional arrays.

2. Use the array_column function

After PHP5.5, we can separate the columns of the two-dimensional array into a one-dimensional array by using the array_column function. The following is an example of using the array_column function to separate a two-dimensional array into a one-dimensional array:

$twoDimensionalArray = array(
    array('id' => 1, 'name' => 'apple', 'price' => 3.5),
    array('id' => 2, 'name' => 'banana', 'price' => 2.5),
    array('id' => 3, 'name' => 'orange', 'price' => 4.0)
);

$idArray = array_column($twoDimensionalArray, 'id');
$nameArray = array_column($twoDimensionalArray, 'name');
$priceArray = array_column($twoDimensionalArray, 'price');

print_r($idArray);
print_r($nameArray);
print_r($priceArray);

In this example, we use the array_column function to separate each column of the two-dimensional array $twoDimensionalArray into a one-dimensional array $idArray, $nameArray and $priceArray. Using this function avoids using a foreach loop to traverse the array and is more concise and easier to maintain.

3. Use the array_map function

In PHP, we can also use the array_map function to divide a two-dimensional array into multiple one-dimensional arrays. The following is an example of using the array_map function to divide a two-dimensional array into a one-dimensional array:

$twoDimensionalArray = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);

$firstArray = array_map(function($arr) { return $arr[0]; }, $twoDimensionalArray);
$secondArray = array_map(function($arr) { return $arr[1]; }, $twoDimensionalArray);
$thirdArray = array_map(function($arr) { return $arr[2]; }, $twoDimensionalArray);

print_r($firstArray);
print_r($secondArray);
print_r($thirdArray);

In this example, we use the array_map function to divide the two-dimensional array $twoDimensionalArray into three one-dimensional arrays $firstArray, $secondArray and $thirdArray. We separate the elements of each one-dimensional array in the array and return it as a new one-dimensional array by using an anonymous function.

Through the above three methods, we can divide the two-dimensional array into multiple one-dimensional arrays to facilitate better manipulation and processing of data. Using these methods can reduce the amount of code, improve the readability and maintainability of the code, and also make the program performance more efficient.

The above is the detailed content of How to divide 2D array into 1D array php. 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