How do you print an alphabetical ordered array of letters from a to z in php?

I made a constant time function as follows

This function gives the Alphabetic representation of a numeric index

public static $alpha = ['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'];

public static function getColName[$index]{
  $index--;
  $nAlphabets = 26;
  $f = floor[$index/pow[$nAlphabets,0]] % $nAlphabets;
  $s = [floor[$index/pow[$nAlphabets,1]] % $nAlphabets]-1;
  $t = [floor[$index/pow[$nAlphabets,2]] % $nAlphabets]-1;

  $f = $f < 0 ? '' : self::$alpha[$f];
  $s = $s < 0 ? '' : self::$alpha[$s];
  $t = $t < 0 ? '' : self::$alpha[$t];

  return trim["{$t}{$s}{$f}"];

}

Now if you want to use it create a range. you can call this function in a loop pushing your values to an array.

As for most of the time, we need the representation rather than a range this function would work just fine.

HOW TO USE

Just enclose these static functions in a class and use it as

className::getColName[47];

Making a range in my case was a waste of memory.

All alphabetic characters in an array can be achieved by using chr[], range[] with for and foreach loop in PHP. To display the array elements as output we can use echo, print_r[] and var_dump[] function.

Using range[] function: This function is used to create an array of elements of any kind such as integer, alphabets within a given range [from low to high] i.e, the first element of list is considered as low and last one is considered as high. It returns an array of alphabets if the range from A to Z i.e. range[A, Z].

Syntax:

array range[ mixed first, mixed second, number steps ]

Example 1: Below example illustrate how to display an array of all alphabetic character using range[] function.

Output:

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,

Example 2: Below example illustrate how to display an array of all Alphabetic character using range[] function along with array_combine.

Output:

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

Using chr[] function: The chr[] function is used to convert an ASCII value to a character. It accepts an ASCII value as a parameter and returns a string representing a character from the specified ASCII value. The ASCII value can be specified in decimal, octal, or hex values. It returns an array of alphabet if the chr[] has its ASCII values from 65 to 91 for alphabet.

Syntax:

string chr[ int $value ]

Example 1: Below example illustrate to display an array of all Alphabetic character using ASCII values.

Output:

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,

Example 2: Below example illustrate to display an array of all Alphabetic character using ASCII values with the help of chr[] function.

Output:

65 => A, 66 => B, 67 => C, 68 => D, 69 => E, 70 => F, 71 => G,
72 => H, 73 => I, 74 => J, 75 => K, 76 => L, 77 => M, 78 => N,
79 => O, 80 => P, 81 => Q, 82 => R, 83 => S, 84 => T, 85 => U,
86 => V, 87 => W, 88 => X, 89 => Y, 90 => Z,


How can we create an array of letters of the alphabets in PHP?

All alphabetic characters in an array can be achieved by using chr[], range[] with for and foreach loop in PHP. To display the array elements as output we can use echo, print_r[] and var_dump[] function.

How to get numeric position of alphabets in PHP?

$my_alphabet = "A" ; I should be able to get the position of the alphabet. i.e. 1..
Even though the letters at location +1 each in ascii a problem arises if uppercase AND lowercase characters can be found as b is not ord[A]+1 but ord[a] +1. ... .
@ThomasE. or just use a strtoupper[] < updated answer. ... .
bwoebi -thanks ...

How can I get next letter in PHP?

“php get next alphabet letter” Code Answer.
$str = 'a';.
echo ++$str; // prints 'b'.
$str = 'z';.
echo ++$str; // prints 'aa'.

Chủ Đề