


How to convert a two-dimensional array into a one-dimensional array in php
Apr 23, 2023 am 10:23 AMIn 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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices?

Can PHP array deduplication take advantage of key name uniqueness?

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code?
