Get Length of Longest String in PHP Array



The array_map function can be used to get the length and the max function can be used to get the length of the longest string.

Below is a code sample for the same −

$max_len = max(array_map('strlen', $array));

Example

 Live Demo

$array = array("a", "Ab", "abcd", "abcdfegh", "achn");
$max_len = max(array_map('strlen', $array));
echo $max_len;

Output

This will produce the following output −

8
Updated on: 2020-04-07T11:30:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements