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?
Another way:
$c = 'A';
$chars = array($c);
while ($c < 'Z') $chars[] = ++$c;
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')));
$alphabets = range('A', 'Z');
$doubleAlphabets = array();
$count = 0;
foreach($alphabets as $key => $alphabet)
{
$count++;
$letter = $alphabet;
while ($letter <= 'Z')
{
$doubleAlphabets[] = $letter;
++$letter;
}
}
return $doubleAlphabets;
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);
To get both upper and lower case merge the two ranges:
$alphas = array_merge(range('A', 'Z'), range('a', 'z'));
<?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
...