How to display an Array under alphabetical letters using PHP?

后端 未结 6 1995
傲寒
傲寒 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.

    
    

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

提交回复
热议问题