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
PHPzOriginal
2023-04-23 10:23:24870browse

In PHP, array is a very common and important data type. Arrays can be divided into one-dimensional, two-dimensional and multi-dimensional forms. In this article, we will learn how to convert a 2D array into a 1D array.

One-dimensional array and two-dimensional array

In PHP, a one-dimensional array is an array with only one dimension. The elements can be any type of data, such as numbers, strings, objects, etc. .

For example:

$array1 = array("dog", "cat", "horse", "cow");

This array contains 4 elements, each element is string type data.

A two-dimensional array refers to an array containing two dimensions. The innermost elements of the array must be of the same data type.

For example:

$array2 = array(
   array("dog", "puppy"),
   array("cat", "kitten"),
   array("horse", "foal"),
   array("cow", "calf")
);

This two-dimensional array contains 4 elements, each element is a subarray containing 2 elements. Each subarray contains one element of type string and one element of type string.

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

When we need to process data, we often need to convert a two-dimensional array into a one-dimensional array. PHP provides a variety of methods to complete this task. The following are three methods:

Method 1: Use the "foreach" loop to traverse

First, we can use the foreach loop to traverse the two-dimensional array elements and then insert each element into a new one-dimensional array.

The following is the sample code:

$array2 = array(
   array("dog", "puppy"),
   array("cat", "kitten"),
   array("horse", "foal"),
   array("cow", "calf")
);

//创建一个新的一维数组
$array1 = array();

//遍历二维数组并插入一维数组中
foreach($array2 as $value) {
   foreach($value as $element) {
      array_push($array1, $element);
   }
}

//打印结果
print_r($array1);

In the above code, we use two foreach loops to iterate through the elements in the two-dimensional array and insert each element into the array. The final printed result is a one-dimensional array.

Method 2: Use the "array_reduce()" function

Another method is to use PHP's "array_reduce()" function, which can also convert a two-dimensional array into a one-dimensional array.

The following is a sample code:

$array2 = array(
   array("dog", "puppy"),
   array("cat", "kitten"),
   array("horse", "foal"),
   array("cow", "calf")
);

//调用array_reduce()函数
$array1 = array_reduce($array2, 'array_merge', array());

//打印结果
print_r($array1);

In the above code, we use PHP's "array_reduce()" function and pass it three parameters: $array2 represents the data that needs to be converted Two-dimensional array, 'array_merge' represents a callback function used to insert each sub-array into a one-dimensional array, array() represents a one-dimensional array with an initial value of empty. The result returned by the function is an array that has been converted into a one-dimensional array.

Method 3: Use the "array_walk_recursive()" function

The last method is to use the "array_walk_recursive()" function, which can help us traverse all the elements in the two-dimensional array, and then Each element is inserted into a new one-dimensional array.

The following is a sample code:

$array2 = array(
   array("dog", "puppy"),
   array("cat", "kitten"),
   array("horse", "foal"),
   array("cow", "calf")
);

//创建一个新的一维数组
$array1 = array();

//调用array_walk_recursive()函数
function convert($value, $key, &$array1) {
   array_push($array1, $value);
}
array_walk_recursive($array2, 'convert', $array1);

//打印结果
print_r($array1);

In the above code, we first create an empty array as the target one-dimensional array, and then call the "array_walk_recursive()" function to traverse the two-dimensional array. The first parameter of the function represents the array that needs to be traversed, the second parameter is a callback function, which is used to insert each element traversed into the one-dimensional array, and the third parameter is the target one-dimensional array. The return value of the function is void, so there is no need to assign the return result to a variable.

Summary

Through the above three methods, we can convert a two-dimensional array into a one-dimensional array more conveniently. It can be seen that in PHP, array is a very commonly used data type, and it is very important to master the relevant skills.

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!

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