$arr = ["Ceylon", "Fish", "Apple", "MongoDB", "Zoomer-19"]; $result = []; $length = 0; array_map(function($v) use (&$length) { if ($length < mb_strlen($v)) { $length = mb_strlen($v); } }, $arr); foreach ($arr as $key => $value) { if ($length == mb_strlen($value)) { $result[] = $value; } } echo join(" ",$result); 

The code is clear to me in principle, but this part of the code is not quite

 array_map(function($v) use (&$length) { if ($length < mb_strlen($v)) { $length = mb_strlen($v); } }, $arr); 

If you can easily explain why this part of the use (&$length) code and this and why is it behind the brackets?

 }, $arr); 
  • Is it like searching for elements of minimum length? the minimum length itself is easier to find in min(array_map('mb_strlen', $arr)) - teran
  • I can do that, but there are only 3 functions here, so try reading the documentation on them. - teran
  • $ arr = ["Ceylon", "Fish", "Apple", "Zoomer-19", "soonminz"]; $ result = []; $ length = 0; min (array_map ('mb_strlen', $ arr)); foreach ($ arr as $ key => $ value) {if ($ length == mb_strlen ($ value)) {$ result [] = $ value; }} var_dump ($ result); displays array (0) {} - Sad
  • $length = min(...) - teran
  • Thanks, it helped, but the output is only 1 minimum / maximum values, I want it to output, all the minimum or maximum values ​​of the array are as follows: arr = ["12", "1", "123"] or arr = ["1234", " 1234567 "," 1234567899 "] - Sad

1 answer 1

What is this part of the use (& $ length) code for?

The use() operator imports variables from the global scope into the local one. Ampersand & , before the variable $length , indicates that the value in the local area is passed by reference. In other words, if the value of the $length variable is changed in the function body, these changes will be reflected in the global scope as well.

why is she out of the box?

Because use() not an argument to a callback function.