duandang2123 2011-08-27 04:26
浏览 46

如何从PHP 4.0中的数组返回最长的字符串? 我尝试了最大功能,但似乎抛出异常

I would like to return the longest string in an array in php 4.0 my sample code looks like this.

$MyArray=Array("Jane","Magdalene","Bull fighting champion","cruising","Tommy Lee Jones","View","axe");
$largest = max($MyArray); 
echo $largest.
  • 写回答

4条回答 默认 最新

  • duanke2012 2011-08-27 04:34
    关注

    max() is to give the maximum from a list of integers. Unfortunately, your problem is more complicated.

    <?php
    $MyArray=Array("Jane","Magdalene","Bull fighting champion","cruising","Tommy Lee Jones","View","axe");
    $maxlen = 0;
    $idx = -1;
    for ($i=count($MyArray); $i; $i--) {
        $len = strlen($MyArray[$i-1]);
        if ($len > $maxlen) {
            $maxlen = $len;
            $idx = $i-1;
        };
    }
    if ($idx >0) {
        echo $MyArray[$idx];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统