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

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

function longest_string_in_array($array) {
  $mapping = array_combine($array, array_map('strlen', $array));
  return array_keys($mapping, max($mapping));

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

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

[ Sources ]

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