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

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

PHPz
Release: 2023-04-19 11:09:59
Original
1316 people have browsed it

In PHP, a multidimensional array refers to an array structure that contains another or multiple arrays. For example:

$array = array(
  array('name' => 'John', 'age' => 20),
  array('name' => 'Mary', 'age' => 25),
  array('name' => 'Mark', 'age' => 30)
);
Copy after login

This is a two-dimensional array containing three elements, each element Is a subarray containing two key-value pairs "name" and "age". If you want to convert this two-dimensional array into a one-dimensional array, you can use some functions in PHP to achieve it.

Method 1: Use the array_column() function

PHP’s array_column() function can extract the value corresponding to the specified key of each element in the array and return a new array containing these values. . If the specified key does not exist, the element is ignored. We can extract the values ​​corresponding to the "name" keys of all elements in the two-dimensional array and obtain a one-dimensional array containing three elements:

$array = array(
  array('name' => 'John', 'age' => 20),
  array('name' => 'Mary', 'age' => 25),
  array('name' => 'Mark', 'age' => 30)
);

$names = array_column($array, 'name');
print_r($names);  // 输出:Array ( [0] => John [1] => Mary [2] => Mark )
Copy after login

Method 2: Use the array_reduce() function

PHP's array_reduce() function can iterate an array according to the rules of a callback function to produce a final value. We can use this function to merge the subarrays of each element in the two-dimensional array into a one-dimensional array:

$array = array(
  array('name' => 'John', 'age' => 20),
  array('name' => 'Mary', 'age' => 25),
  array('name' => 'Mark', 'age' => 30)
);

$flatten = array_reduce($array, function($carry, $item) {
  return array_merge($carry, $item);
}, array());
print_r($flatten);  // 输出:Array ( [name] => Mark [age] => 30 )
Copy after login

In the above code, the array_reduce() function accepts a callback function and an initial value. The parameter $carry of the callback function represents the current result, and $item represents the value of the current element. The initial value is an empty array, which means that the final result array starts iterative processing with the empty array as the initial value. The array_merge() function is used in the callback function to merge the subarray of the current element and the previous result array.

It should be noted that if each element in the two-dimensional array has a different number of key-value pairs, the above method 2 can only get all the key-value pairs of the last element. In more stringent situations, the subarray can be verified before looping to avoid losing data.

The above are two methods of converting PHP multi-dimensional arrays into one-dimensional arrays. Choosing the appropriate method according to the specific occasion and data structure can increase the efficiency and readability of the code.

The above is the detailed content of How to convert multi-dimensional array to one-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template