How to display an Array under alphabetical letters using PHP?

后端 未结 6 1985
傲寒
傲寒 2021-02-04 17:06

I have an array that already contains all it\'s values in alphabetical order:

Alligator
Alpha
Bear
Bees
Banana
Cat
Cougar

Now I just want to li

相关标签:
6条回答
  • 2021-02-04 17:26

    Well i have three solutions. 1) Make another array containing all alphabets. Then use foreach to iterate through this array. And in nested foreach check for occurance of that letter through strpos method of php. Here is rough code.

    <?php
    $alphabets = array ("A","B","C"....); //for all alphabtes
    foreach($alphabets as $alphabet)
    {
        echo $alphabet;
        foreach($myArray as $arr)
        {
             $pos = strpos($arr, $alphabet);
             if($pos===flase)
             {
                 //do nothing
             }
             else
             {
                 echo $arr;
             }
    }
    ?>
    

    2) second method have same logic as above. But here you dont need to make array for alphabets. You can get all alphabets this way.

    <?php
    foreach(range('a', 'z') as $letter) {
        echo $letter;
    }
    ?>
    

    Php range method

    3) Third solution also have same logic as above two. Here you can get alphabets by another way :)

    for ($i=65; $i< =90; $i++) {
     $x = chr($i);
     print $x;
    }
    
    0 讨论(0)
  • 2021-02-04 17:27

    For HTML output:

    <h3>A</h3>
    <ol>
       <li>
          <em>tag count</em>
          <a href="link to tag">Animal</a>
       </li>
       <li>
          <em>tag count</em>
          <a href="link to tag">Aqua</a>
       </li>
       <li>
          <em>tag count</em>
          <a href="link to tag">Arthur</a>
       </li>
    </ol>
    <!-- if B not EXIST not show B -->
    <h3>C</h3>
    <ol>
       <li>
          <em>tag count</em>
          <a href="link to tag">Camel</a>
       </li>
       <li>
          <em>tag count</em>
          <a href="link to tag">Crazy</a>
       </li>
    </ol>
    <!-- etc -->
    

    I change Artefact2 code and share for you

    <?php $previous = null;
        foreach ($dataProvider as $key => $tag) { 
            $firstLetter = strtoupper(substr($tag['name'], 0, 1));
            if($previous !== $firstLetter) { 
                if($key != 0) {
                    echo '</ol>';
                }?>
                <h3 class="alpha">
                    <span><?php echo $firstLetter;?></span>
                </h3>
                <ol class="tags main">
            <?php } ?>
                    <li class="tag">
                        <em><?php echo $tag['TagsCount']; ?></em>
                        <a href="<?php echo $tag['slug']; ?>">
                            <strong><?php echo ucfirst($tag['name']); ?></strong>
                        </a>
                        <span class="perc" style="width: 90px;"></span>
                    </li>
            <?php if($key == count($dataProvider)-1) { 
                echo '</ol>';
            }
                $previous = $firstLetter; 
        }
    ?>
    

    Best Regards www.Forum.iSYSTEMS.am

    0 讨论(0)
  • 2021-02-04 17:32

    Iterate through the array and check if the current item starts with another letter than the previous one. If that's the case print your "A ---".

    $currentLetter = '';
    foreach ($list as $item) {
       $firstLetter = substr($item, 0, 1);
       if ($firstLetter !== $currentLetter) {
          echo $firstLetter . "\n---\n";
          $currentLetter = $firstLetter;
       }
       echo $item . "\n";
    }
    
    0 讨论(0)
  • 2021-02-04 17:33

    The solution is to keep in a variable the first letter of the previously printed word, like:

    $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";
    }
    

    NB: if some entries start with a lower-case letter and others with upper-case letters, use the strcasecmp function in the test instead of !==.

    0 讨论(0)
  • 2021-02-04 17:33

    here is another simple solution:-

    $result = array();
    foreach ($list as $item) {
        $firstLetter = substr($item, 0, 1);
        $result[$firstLetter][] = $item;
    }
    
    echo "<pre>"; print_r($result);
    

    Output :-

    Array (
    [A] => Array
        (
            [0] => Alligator
            [1] => Alpha
        )
    
    [B] => Array
        (
            [0] => Bear
            [1] => Bees
            [2] => Banana
        )
    
    [C] => Array
        (
            [0] => Cat
            [1] => Cougar
        )
     )
    
    0 讨论(0)
  • 2021-02-04 17:53

    As Shivansh said above in his answer, I think that is correct way

    $result = array();
    foreach ($list as $item) {
        $firstLetter = substr($item, 0, 1);
        $result[$firstLetter][] = $item;
    }
    
    echo "<pre>"; print_r($result);
    

    To Display the array generated by this code use

    foreach ( $list as $key => $value ) {
        //Do some thing with $key (A,B,C)
        foreach ($value as $var){
            //Do some thing with $var (Array of A, B ,C)
        }
    }
    
    0 讨论(0)
提交回复
热议问题