How to return to the larger element in the array?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
19 views
property is an array
[ "World", "Hello", "Hello World" ]

I need to return the greatest number of letters the item. But as I can not figure out.
by | 19 views

2 Answers

0 like 0 dislike
$array = [ "World", "Hello", "Hello World" ]; echo array_reduce($array, function($res, $item) { return mb_strlen($res) < mb_strlen($item) ? $item : $res; }, ");
by
0 like 0 dislike
$array = [ "World", "Hello", "Hello World" ]; $last_length = 0; $last_key = null; foreach ($array as $key => $value) { if (mb_strlen($value) > $last_length) { $last_key = $key; } } var_dump($array[$last_key]);
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
36,596 users