In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
PHP traversing the array of six ways are, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Arrays are the most common structure in the daily operation of PHP, and we deal with array-related contents almost every day. So the question is, how do you usually traverse and deal with arrays.
1 、 foreach
It's familiar, isn't it? is it your favorite?
$arr = ['arr as,' baked,'c']; foreach ($arr $key = > $value) {$arr [$key] = $value. '_ iTunes;} print_r ($arr); / / [' axioms, 'bachelors,' canalias]; 2. Array_map
Use anonymous functions to process each element in the array. Return a new array after traversal
$arr = ['await,' baked,'c']; $arr = array_map (function ($item) {return $item. '_ iTunes;}, $arr); print_r ($arr); / / [' axioms, 'breadwinners,' canalias]
You can process multiple arrays at the same time.
$arr_1 = ['item_1,' baked,'c']; $arr_2 = ['you', 'good','Mo', 'original', 'sin']; / / $arr_n = [...]; $arr = array_map (function ($item_1, $item_2) {return $item_1. '_'. $item_2. '_ iTunes;}, $arr_1, $arr_2); print_r ($arr); / / [' a _ you _ ibis,'b _ good _ ibis,'c _ Mo _ iones,'_ original _ ibis,'_ crime _ ibis]
Yes, the number of iterations is based on the longest array.
After 3 iterations, $arr_1 is actually complete, so then the value of $item_1 is null.
3 、 array_walk
Iterate through your array in the form of reference passing, with no return value, and only one array can be processed.
$arr = ['arr,' baked,'c']; array_walk ($arr, function (& $item) {$item = $item. '_ iTunes;}); print_r ($arr); / / [' axioms, 'breadwinners,' canalias]
Have you noticed & $item? yes, there is an extra &, which means that your parameter is passing and receiving data by reference. All you have to do is find a way to change this parameter.
If you are dealing with a two-dimensional array, the way you deal with it is the same.
$arr = [['name' = >' a'], ['name' = >' b'], ['name' = >' c']]; array_walk ($arr, function (& $item) {$item ['name'] = $item [' name']. '_ name';}); print_r ($arr); / / [[' name' = > 'afanti'], [' name' = > 'breadi`], [' name' = > 'celibate]]; 4. For
This should be a super universal grammar.
$arr = ['arr,' baked,'c']; for ($I = 0; $I < count ($arr); + + $I) {$arr [$I] = $arr [$I]. '_ iTunes;} print_r ($arr); / / [' axioms, 'breadwinners,' canalias]
It is not recommended. It is not as efficient as foreach. And the writing is a little more tedious than foreach.
5 、 each
Haha, this function has been abandoned @ deprecated in PHP7.2, and it feels particularly old-fashioned. The execution efficiency of this kind of writing is not good either, and the novice should give up directly.
$arr = ['await,' baked,'c']; while (list ($key, $value) = each ($arr)) {$arr [$key] = $value. '_ iTunes;} print_r ($arr); / / [' axioms, 'bachelors,' cages']; 6. Reset
To be exact, there is not only one function of reset, it needs to combine several built-in functions, so it is basically impossible to write code.
$arr = ['value,' baked,'c']; / / reset () the pointer switches to the header. In fact, the default is to point to the beginning, and you can ignore this reset ($arr); / / current () gets the element value of the current pointer while ($value = current ($arr)) {/ / key () gets the element subscript of the current pointer $arr [key ($arr)] = $value. Move the pointer to the next next ($arr);} print_r ($arr); / / ['axioms,' breadibles, 'cations']; is it helpful for you to finish reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.