How to convert 2D array to 1D array in PHP
How to convert a two-dimensional array to a one-dimensional array in PHP
In PHP development, you often encounter scenarios where you need to convert a two-dimensional array into a one-dimensional array. This article will introduce several common methods to help you complete this task easily.
Method 1: Use loop traversal
The simplest and most straightforward method is to use a loop to traverse the two-dimensional array and add each element to a new one-dimensional array. The following is a code example using this method:
function flattenArray($array) { $result = []; foreach ($array as $subArray) { foreach ($subArray as $element) { $result[] = $element; } } return $result; } // 测试 $twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; $oneDimensionalArray = flattenArray($twoDimensionalArray); print_r($oneDimensionalArray);
Run the above code, the output should be:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )
Method 2: Use the array_reduce function
PHP provides a powerful The array_reduce function can be used to convert a two-dimensional array into a one-dimensional array. The array_reduce function accepts an array to be processed and a callback function as parameters, which is used to decide how to gradually reduce the values in the array to a single value. The following is a code example using the array_reduce function:
function flattenArray($array) { return array_reduce($array, function($carry, $item) { return array_merge($carry, $item); }, []); } // 测试 $twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; $oneDimensionalArray = flattenArray($twoDimensionalArray); print_r($oneDimensionalArray);
Run the above code, the output should be the same one-dimensional array.
Method 3: Use the array_merge function
The array_merge function can also be used to convert a two-dimensional array into a one-dimensional array. The array_merge function merges multiple arrays into one array and returns the result. The following is a code example using the array_merge function:
function flattenArray($array) { return call_user_func_array('array_merge', $array); } // 测试 $twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; $oneDimensionalArray = flattenArray($twoDimensionalArray); print_r($oneDimensionalArray);
Run the above code, the output should be the same one-dimensional array.
Method 4: Use the array_walk_recursive function
The array_walk_recursive function is used to recursively traverse each element in the array and perform callback processing. The following is a code example using the array_walk_recursive function:
function flattenArray(&$array) { $result = []; array_walk_recursive($array, function($item) use (&$result) { $result[] = $item; }); return $result; } // 测试 $twoDimensionalArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; $oneDimensionalArray = flattenArray($twoDimensionalArray); print_r($oneDimensionalArray);
Run the above code, the output should be the same one-dimensional array.
Summary
This article introduces four common methods to convert a two-dimensional array into a one-dimensional array in PHP. Choosing a method that suits your project needs and personal habits can improve the readability and efficiency of your code. The first of these methods is the most common and straightforward, but other methods may be more effective in certain scenarios. I hope this article can help you better understand and apply these methods to easily convert two-dimensional arrays to one-dimensional arrays during development.
The above is the detailed content of How to convert 2D array to 1D array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
