<?php

// Perhaps the simplest practical use of a fold?
//
// (While this is fairly consise (even in PHP) it calculates the length of a
// string 2n-2 times [which is n-2 times more than needed])
function getLongestWord($words)
{
  $fun = create_function('$a,$b', 'return (strlen($a) > strlen($b)) ? $a : $b;');

  return array_reduce($words, $fun);
}

// This looks a little better, but it loses a lot of the simplicity of the above
function getLongestWord_2($words)
{  
  $map  = create_function('$a', 'return array($a, strlen($a));');
  $fold = create_function('$a,$b', 'return ($a[1] > $b[1]) ? $a : $b;');

  $word = array_reduce(array_map($map, $words), $fold);

  return $word[0];
}

$words = array('a', 'bb', 'ccc', 'dd', 'eeee', 'fff', 'gg');

var_dump(getLongestWord($words));
// string(4) "eeee"

var_dump(getLongestWord_2($words));
// string(4) "eeee"

?>

Is there an even simpler way to find the longest string in an array?