Way to get all alphabetic chars in an array in PHP?

后端 未结 14 672
情话喂你
情话喂你 2020-12-22 20:44

Is there a way to get all alphabetic chars (A-Z) in an array in PHP so I can loop through them and display them?

相关标签:
14条回答
  • 2020-12-22 21:19

    Another way:

    $c = 'A';
    $chars = array($c);
    while ($c < 'Z') $chars[] = ++$c;
    
    0 讨论(0)
  • 2020-12-22 21:25

    Try this :

    function missingCharacter($list) {
    
            // Create an array with a range from array minimum to maximu
            $newArray = range(min($list), max($list));
    
            // Find those elements that are present in the $newArray but not in given $list
            return array_diff($newArray, $list);
        }
    print_r(missCharacter(array('a','b','d','g')));
    
    0 讨论(0)
  • 2020-12-22 21:26
    $alphabets = range('A', 'Z');
        $doubleAlphabets = array();
        $count = 0;
        foreach($alphabets as $key => $alphabet)
        {
            $count++;
            $letter = $alphabet;
            while ($letter <= 'Z') 
            {
                $doubleAlphabets[] = $letter;
    
                ++$letter;
            }
        }
    
        return $doubleAlphabets;
    
    0 讨论(0)
  • 2020-12-22 21:27

    PHP has already provided a function for such applications.
    chr(x) returns the ascii character with integer index of x.
    In some cases, this approach should prove most intuitive.
    Refer http://www.asciitable.com/

    $UPPERCASE_LETTERS = range(chr(65),chr(90));
    $LOWERCASE_LETTERS = range(chr(97),chr(122));
    $NUMBERS_ZERO_THROUGH_NINE = range(chr(48),chr(57));
    
    $ALPHA_NUMERIC_CHARS = array_merge($UPPERCASE_LETTERS, $LOWERCASE_LETTERS, $NUMBERS_ZERO_THROUGH_NINE); 
    
    0 讨论(0)
  • 2020-12-22 21:28

    To get both upper and lower case merge the two ranges:

    $alphas = array_merge(range('A', 'Z'), range('a', 'z'));
    
    0 讨论(0)
  • 2020-12-22 21:30
    <?php 
    
    $array = Array();
    for( $i = 65; $i < 91; $i++){
            $array[] = chr($i);
    }
    
    foreach( $array as $k => $v){
            echo "$k $v \n";
    }
    
    ?>
    
    $ php loop.php 
    0 A 
    1 B 
    2 C 
    3 D 
    4 E 
    5 F 
    6 G 
    7 H
    ...
    
    0 讨论(0)
提交回复
热议问题