S
S
Sergey Beloventsev2018-04-27 22:29:34
PHP
Sergey Beloventsev, 2018-04-27 22:29:34

How to return by and larger element in an array?

actually here is the array

[
"World",
"Hello",
"Hello World"
]

I need to return the largest element by the number of letters. How I can't imagine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2018-04-27
Hrebet @hrebet

$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]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question