Home Backend Development PHP Problem How to convert two-dimensional array to one-dimensional in php

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

Apr 19, 2023 am 11:41 AM

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);
    }
}
Copy after login

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);
});
Copy after login

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');
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1240
24