PHP: Get the longest string (or length) in an array

0 Comments

To quickly get the longest string in an array, use the following function.

1.function longest_string_in_array($array) {   
2.  $mapping = array_combine($array, array_map('strlen', $array));    
3.  return array_keys($mapping, max($mapping));    
4.}

If you need the length of the longest string, use this little snippet.

1.$maxlen = max(array_map('strlen', $array));

[ Sources ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog