In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains how php converts a two-dimensional array into an one-dimensional array. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "php how to convert a two-dimensional array into an one-dimensional array".
Php two-dimensional array to one-dimensional array method: 1, with array_column () function; 2, with array_walk () function; 3, with array_map () function; 4, with array_reduce () function; 5, with array_walk_recursive () function.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Summary of two-dimensional array to one-dimensional array
For example, convert the following two-digit array to an one-dimensional array
$records = [['id' = > 2135,' first_name' = > 'John',' last_name' = > 'Doe',], [' id' = > 3245, 'first_name' = >' Sally', 'last_name' = >' Smith',], ['id' = > 5342,' first_name' = > 'Jane'] 'last_name' = >' Jones',], ['id' = > 5623,' first_name' = > 'Peter',' last_name' = > 'Doe',]]
1.array_column ()
Array_column () is a PHP built-in function, and the restriction is that the PHP version must be 5.5.0 or above!
Example 1:
The print result is as follows:
Example 2:
The print result is as follows:
2.array_walk ()
The array_walk () function uses a user-defined function to callback each element in the array to achieve the current function:
$first_names= []; array_walk ($records, function ($value, $key) use (& $first_names) {$first_names [] = $value ['first_name'];}); var_dump ($first_names)
The print result is as follows:
3.array_map ()
The array_map () function acts similarly to array_walk (), applying the callback function to the cells of a given array.
$first_names= []; array_map (function ($value) use (& $first_names) {$first_names [] = $value ['first_name'];}, $records); var_dump ($first_names)
The print result is as follows:
4.array_reduce ()
Array_reduce-use the callback function to iteratively simplify the array to a single value.
$first_names = array_reduce ($records,function ($result, $value) {array_push ($result, $value ['first_name']); return $result;}, []); var_dump ($first_names)
Print the results:
5. Array_walk_recursive ()
Array_walk_recursive-applies the user function recursively to each member of the array. This function can convert an array of any dimension into an one-dimensional array.
At this point, I believe you have a deeper understanding of "php how to turn a two-dimensional array into an one-dimensional array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.