【问题标题】:Print alphabetical navigation list from an array从数组中打印按字母顺序排列的导航列表
【发布时间】:2013-08-23 06:02:36
【问题描述】:

我有一个项目数组,我正在尝试为它们生成一个按字母顺序排列的导航。

A|B|C|D|E|F|G|H 等...

  • 苹果
  • 杏子
  • 胡萝卜
  • 骆驼

所以我想列出字母表中的每个字母,但只链接数组中匹配项的那些。

到目前为止我有:

$productArr = array('Apple','Apricot','Carrot','Camel','Dog');

$previous = null;
foreach(range('A','Z') as $alpha) {
    $arrayCount = count($productArr);
    for ($i=0; $i < $arrayCount; $i++) {
        $firstLetter    = $productArr[$i];
        if ($firstLetter[0] == $alpha && $firstLetter[0] != $previous){
            echo '<li><a href="#'.$alpha.'">'.$alpha.'</a></li>';
            $previous = $alpha;
        }elseif ($firstLetter[0] != $alpha && $alpha != $previous){
            echo '<li>'.$alpha.'</li>';
            $previous = $alpha;
        }
    }       
}

elseif 之前它工作正常,如果您注释掉 elseif,它会按预期打印链接列表。只需要弄清楚如何打印字母表的其余部分。

任何关于我哪里出错的帮助将不胜感激。

干杯

【问题讨论】:

  • 所以我想列出字母表中的每个字母,但只链接数组中具有匹配项的那些意味着您只想链接那些具有以该字母表开头的数组值的字母表。如果我错了。

标签: php arrays loops


【解决方案1】:

我认为秘诀是将 elseif 移出内部循环。

<?php
$productArr = array('Apple','Apricot','Carrot','Camel','Dog', 'Rabbbit');

$previous = null;
foreach(range('A','Z') as $alpha) {
    $arrayCount = count($productArr);
    $printed = false;
    for ($i=0; $i < $arrayCount; $i++) {
        $firstLetter    = $productArr[$i];
        if ($firstLetter[0] == $alpha && $firstLetter[0] != $previous){
            echo '<li><a href="#'.$alpha.'">'.$alpha.'</a></li>';
            $previous = $alpha;
            $printed = true;
        } 
    }
    if($printed == false){
         echo '<li>'.$alpha.'</li>';
    }
}
?>

【讨论】:

    【解决方案2】:

    您需要一个额外的步骤来首先对产品进行分组。

        $productArr = array('Apple','Apricot','Carrot','Camel','Dog');
    
        //This is the extra step
        $productArrGroup = array();
        foreach($productArr as $product){
            if(!isset($productArrGroup[$product[0]])){
                $productArrGroup[$product[0]] = array();
            }
            $productArrGroup[$product[0]][] = $product;
        }
    
        //You can even print the number of matching product for each $alpha like this !
        foreach(range('A','Z') as $alpha) {
            if(array_key_exists($alpha, $productArrGroup)){
                echo '<li><a href="#'.$alpha.'">'.$alpha.' ('.count($productArrGroup[$alpha]).')</a></li>'."\n";
            }else{
                echo '<li>'.$alpha.'</li>'."\n";
            }
        }
    

    以上代码的结果

    <li><a href="#A">A(2)</a></li>
    <li>B</li>
    <li><a href="#C">C(2)</a></li>
    <li><a href="#D">D(1)</a></li>
    <li>E</li>
    <li>F</li>
    <li>G</li>
    <li>H</li>
    <li>I</li>
    <li>J</li>
    <li>K</li>
    <li>L</li>
    <li>M</li>
    <li>N</li>
    <li>O</li>
    <li>P</li>
    <li>Q</li>
    <li>R</li>
    <li>S</li>
    <li>T</li>
    <li>U</li>
    <li>V</li>
    <li>W</li>
    <li>X</li>
    <li>Y</li>
    <li>Z</li>
    

    注意:您可能希望不区分大小写地匹配字母,在这种情况下,您可以将strtoupper 函数用于$product[0]

    【讨论】:

      【解决方案3】:

      请检查这可能有用

      <?php
      // define indexed array
      $animals = array("wolf", "lion", "tiger", "iguana", "bear","zebra", "leopard");
      // sort alphabetically by value
      // result: ("bear", "iguana", "leopard", "lion", "tiger", "wolf","zebra")
      sort($animals);
      print_r($animals);
      ?>
      

      还有这个:

      $previous = null;
      foreach($array as $value) {
          $firstLetter = substr($value, 0, 1);
          if($previous !== $firstLetter) echo "\n".$firstLetter."\n---\n\n";
          $previous = $firstLetter;
      
          echo $value."\n";
      }
      

      【讨论】:

        【解决方案4】:

        您的 elseif 应该是 || 而不是 &amp;&amp;

        elseif ($firstLetter[0] == $alpha && $alpha != $previous){
        

        演示http://phpfiddle.org/main/code/i2q-97d

        【讨论】:

        • 这会打印每个字母 5 次!
        • 但是我们想打印所有的字母......并且只链接那些在数组中的第一个字符......你的答案只打印数组中的那些......如果我错了,请纠正我
        猜你喜欢
        • 1970-01-01
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-15
        相关资源
        最近更新 更多