Manera de obtener todos los caracteres alfabéticos en una matriz en PHP?


¿Hay alguna manera de obtener todos los caracteres alfabéticos (A-Z) en una matriz en PHP para que pueda recorrerlos y mostrarlos?

 152
php
Author: Paul Dixon, 2009-01-11

12 answers

$alphas = range('A', 'Z');
 415
Author: PEZ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-11 16:03:40

Para obtener ambas mayúsculas y minúsculas fusionar los dos rangos:

$alphas = array_merge(range('A', 'Z'), range('a', 'z'));
 57
Author: PEZ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-11 16:04:39
$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
 27
Author: Nathan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-01 08:37:06

Otra manera:

$c = 'A';
$chars = array($c);
while ($c < 'Z') $chars[] = ++$c;
 12
Author: Gumbo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-11 16:17:10

PHP ya ha proporcionado una función para tales aplicaciones.
chr(x) devuelve el carácter ascii con índice entero de x.
En algunos casos, este enfoque debería resultar muy intuitivo.
Consulte 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); 
 8
Author: Kyle Michael Becker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-06 20:10:05
<?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
...
 4
Author: Aaron Maenpaa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-11 12:52:15

Rango para A-Z pero si quieres ir por ejemplo de A a DU entonces:

 function generateAlphabet($na) {
        $sa = "";
        while ($na >= 0) {
            $sa = chr($na % 26 + 65) . $sa;
            $na = floor($na / 26) - 1;
        }
        return $sa;
    }

    $alphabet = Array();
    for ($na = 0; $na < 125; $na++) {
        $alphabet[]=generateAlphabet($na);
    }

    print_r($alphabet);

Su respuesta se verá como:

Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z [26] => AA [27] => AB [28] => AC [29] => AD [30] => AE [31] => AF [32] => AG [33] => AH [34] => AI [35] => AJ [36] => AK [37] => AL [38] => AM [39] => AN [40] => AO [41] => AP [42] => AQ [43] => AR [44] => AS [45] => AT [46] => AU [47] => AV [48] => AW [49] => AX [50] => AY [51] => AZ [52] => BA [53] => BB [54] => BC [55] => BD [56] => BE [57] => BF [58] => BG [59] => BH [60] => BI [61] => BJ [62] => BK [63] => BL [64] => BM [65] => BN [66] => BO [67] => BP [68] => BQ [69] => BR [70] => BS [71] => BT [72] => BU [73] => BV [74] => BW [75] => BX [76] => BY [77] => BZ [78] => CA [79] => CB [80] => CC [81] => CD [82] => CE [83] => CF [84] => CG [85] => CH [86] => CI [87] => CJ [88] => CK [89] => CL [90] => CM [91] => CN [92] => CO [93] => CP [94] => CQ [95] => CR [96] => CS [97] => CT [98] => CU [99] => CV [100] => CW [101] => CX [102] => CY [103] => CZ [104] => DA [105] => DB [106] => DC [107] => DD [108] => DE [109] => DF [110] => DG [111] => DH [112] => DI [113] => DJ [114] => DK [115] => DL [116] => DM [117] => DN [118] => DO [119] => DP [120] => DQ [121] => DR [122] => DS [123] => DT [124] => DU ) 
 4
Author: HellBaby,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-24 14:12:07

Si necesita una matriz que tenga claves alfabéticas, así como elementos (para una lista desplegable alfabética, por ejemplo), podría hacer esto:

$alphas = array_combine(range('A','Z'),range('A','Z'))

Rinde:

array (size=26)
  'A' => string 'A' (length=1)
  'B' => string 'B' (length=1)
  'C' => string 'C' (length=1)
  'D' => string 'D' (length=1)
  ...etc
 3
Author: pbarney,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-20 00:29:14
$array = range('a', 'z');
 1
Author: benlumley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-10 23:19:58

Tal vez sea un poco fuera de lo común (el iniciador del tema pidió la solución solo para la A-Z), pero para la solción de caracteres cirrílicos es:

// to place letters into the array
$alphas = array();
foreach (range(chr(0xC0), chr(0xDF)) as $b) {
    $alphas[] = iconv('CP1251', 'UTF-8', $b);
}    

// or conver array into comma-separated string
$alphas = array_reduce($alphas, function($p, $n) {
  return $p . '\'' . $n . '\',';
});
$alphas = rtrim($alphas, ',');

// echo string for testing
echo $alphas;
// or echo mb_strtolower($alphas); for lowercase letters
 1
Author: userlond,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-10 03:52:31
$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
Author: Mohd Aidi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-09 07:25:46

Letras minúsculas

for ($x = 97; $x < 122; $x++) {
    $y = chr($x);
    echo $y;
    echo "<br>";
}

Letras mayúsculas

for ($x = 65; $x < 90; $x++) {
    $y = chr($x);
    echo $y;
    echo "<br>";
}
 -1
Author: Raja vikrant Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-13 15:09:54