【问题标题】:How Can I Extract the stringlength higher word in an array?如何提取数组中字符串长度较高的单词?
【发布时间】:2014-07-13 00:43:15
【问题描述】:

第一次,我为我糟糕的英语感到抱歉。 经过三天试图解决这个问题,我放弃了。

给出这个数组:

$names = array ( "James Walter Case", "Benjamin Wallace Pinkman", "Billy Elliot Newson" )

我必须提取每个全名的较高第一个字符串长度单词的全名。 在这种情况下,考虑到这种情况,本杰明将是更高的名字。一次 此名称已提取,我必须打印全名。

有什么想法吗?谢谢

【问题讨论】:

  • 循环数组爆炸空间,存储第一个单词的计数,排序或检索最高计数
  • @Dagon 他显然是个初学者(他为此花了 3 天时间)。我猜你只是在跟他说中文。
  • 我们都从某个地方开始。

标签: php max string-length


【解决方案1】:

这个怎么样?

    $names = array ( "James Walter Case", "Benjamin Wallace Pinkman", "Billy Elliot Newson" );

    if (count($names) > 0)
    {
        $higher_score = 0;

        foreach ($names as $name_key => $name_value) 
        {
            $name_array = explode(" ", $name_value);
            // echo("<br><b>name_array -></b><pre><font FACE='arial' size='-1'>"); print_r($name_array); echo("</font></pre><br>");

            $first_name = $name_array[0];
            // echo "first_name -> ".$first_name."<br>";

            $score = strlen($first_name);
            // echo "score -> ".$score."<br>";

            if ($score > $higher_score)
            {
                $higher_score = $score;
                $winner_key = $name_key;                }
        }
        reset($names);
    }

echo("longest first name is ".$names[$winner_key]." with ".$higher_score." letters.");

【讨论】:

    【解决方案2】:

    如前所述,您可以使用array_map 获取第一个字符串的所有长度,然后根据该值的键获取名称。

    $names = array ( "James Walter Case", "Benjamin Wallace Pinkman", "Billy Elliot Newson" );
    
    $x = array_map(
        function ($a) {
            $x = explode (" ", $a); // split the string
            return strlen($x[0]); // get the first name
        }
    , $names);
    
    $maxs = array_keys($x, max($x)); // get the max values (may have equals)
    echo $names[$maxs[0]]; // get the first max
    

    -- 编辑

    实际上,考虑到这种情况,有一种更好的方法可以做到这一点。您可以简单地按名字的长度对数组进行排序,然后获取第一个键:

    usort($names, 
        function ($a, $b) {
            $aa = explode(" ", $a); // split full name
            $bb = explode(" ", $b); // split full name
            if (strlen($aa[0]) > strlen($bb[0])){
                return 0; // if a > b, return 0
            } else {
                return 1; // else return 1
            }
        }
    );
    echo $names[0]; // get top element
    

    【讨论】:

    • 什么?更多的代码行? ;) 这个解决方案也很优雅。
    【解决方案3】:

    请允许我提供手工制作的解决方案。

    <?php
    $maxLength = 0;
    $fullName = "";
    $parts = array();
    
    foreach($names as $name){
        /* Exploding the name into parts.
         * For instance, "James Walter Case" results in the following array :
         *     array("James", "Walter", "Case") */
        $parts = explode(' ', $name);
        if(strlen($parts[0]) > $maxLength){ // Compare first length to the maximum.
            $maxLength = strlen($parts[0]);
            $fullName = $name;
        }
    }
    
    echo "Longest first name belongs to " . $fullName . " (" . $maxLength . " character(s))";
    ?>
    

    基本上,我们遍历数组,将每个名称分成几部分(由空格分隔),并尝试在名称的第一部分中找到最大长度($parts[0])。这基本上是一种最大搜索算法。

    PHP array_* 函数可能有更好的方法,但是,这个方法很容易解释。

    【讨论】:

      【解决方案4】:

      你可以试试……

      $maxWordLength=0;
      $maxWordIndex=null;
      foreach ($names as $index=>$name){
         $firstName=first(explode(' ',$name)); 
         if (strlen($firstName)>maxWordLength){
            $maxWordLength=strlen($firstName);
            $maxWordIndex=$index; 
         }
      }
      
      if ($maxWordIndex){
         echo $names[$maxWordIndex];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        • 2011-05-13
        • 1970-01-01
        • 2020-01-28
        • 1970-01-01
        • 2017-02-14
        • 2012-10-27
        相关资源
        最近更新 更多