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

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

PHPz
PHPzOriginal
2023-04-19 11:41:48914browse

When developing PHP programs, we often need to use array data in the code, and the two-dimensional arrays used are also very common. However, in some cases, we need to convert a 2D array into a 1D array. This is very useful when writing PHP applications. Below are the detailed steps on how to convert a 2D array to a 1D array in PHP.

1. Use foreach to traverse the array

To convert a two-dimensional array into a one-dimensional array, you first need to use the foreach statement to traverse the elements in the array. The following is a sample code for traversal:

$two_dimensional_array = array(
    array('name' => '张三', 'age' => 20),
    array('name' => '李四', 'age' => 21),
    array('name' => '王五', 'age' => 22)
);

$one_dimensional_array = array();

foreach ($two_dimensional_array as $row) {
    foreach ($row as $key => $value){
        array_push($one_dimensional_array, $value);
    }
}

This code converts a two-dimensional array into a one-dimensional array $one_dimensional_array. In the above code, we iterate through each row in $two_dimensional_array and add the value of each row to the $one_dimensional_array array.

2. Use the array_walk_recursive function

In PHP, you can also use the array_walk_recursive function to convert a two-dimensional array into a one-dimensional array. The array_walk_recursive function allows you to walk through all elements in a multidimensional array. The following is a sample code:

$two_dimensional_array = array(
    array('name' => '张三', 'age' => 20),
    array('name' => '李四', 'age' => 21),
    array('name' => '王五', 'age' => 22)
);

$one_dimensional_array = array();

array_walk_recursive($two_dimensional_array, function ($item, $key) use (&$one_dimensional_array) {
    array_push($one_dimensional_array, $item);
});

In the above code, we use the array_walk_recursive function to convert a two-dimensional array into a one-dimensional array $one_dimensional_array. In the callback function, we add each element to the $one_dimensional_array array and use the reference symbol & in the callback function's parameters, allowing us to change the contents of the array while processing it in the function.

3. Use the array_column function

PHP provides a function called array_column, which allows us to extract all the values ​​​​of the specified column in a two-dimensional array and convert it into a one-dimensional array. Below is the sample code:

$two_dimensional_array = array(
    array('name' => '张三', 'age' => 20),
    array('name' => '李四', 'age' => 21),
    array('name' => '王五', 'age' => 22)
);

$one_dimensional_array = array_column($two_dimensional_array, 'name');

In the above code, we extracted the 'name' column of each row using the array_column function and converted it into a new one-dimensional array. This function is very convenient, especially when you need to extract a specific column from a larger two-dimensional array.

Summary:

The above are three methods to convert PHP two-dimensional array to one-dimensional array. You can choose the method that suits you best based on your needs. If you need to process smaller arrays, it is simpler and faster to use the foreach method. If you are dealing with large arrays, there are also benefits to using array_walk_recursive or array_column, which can improve the efficiency of execution.

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