How to get the length of longest string in an array

后端 未结 3 1966
说谎
说谎 2020-12-05 17:32

Say I have this array:

$array[] = \'foo\';
$array[] = \'apple\';
$array[] = \'1234567890;

I want to get the length of the longest string in

3条回答
  •  温柔的废话
    2020-12-05 17:40

    Sure:

    function getmax($array, $cur, $curmax) {
      return $cur >= count($array) ? $curmax :
        getmax($array, $cur + 1, strlen($array[$cur]) > strlen($array[$curmax])
               ? $cur : $curmax);
    }
    
    $index_of_longest = getmax($my_array, 0, 0);
    

    No loop there. ;-)

提交回复
热议问题