PHP: Create an Array of A Range of Characters (Numbers or Letters) or Multiple Identical Values

by Yang Yang on June 2, 2009

Share This Article:
Subscribe to Kavoir: blog feed

To create an array of a series of consecutive numbers or alphabetical letters or even any ASCII characters, you can use PHP function range():

$a = range(0, 100, 2); // $a is an array of even integers from 0 to 100.
$b = range('a', 'z'); // $b now contains all lowercase alphabetic letters from a to z.

To create an array of a series of identical values, use function array_fill():

$dumb = array_fill(-10, 3, 'dumb');

$dumb is as follows:

Array
(
    [-10] => 'dumb'
    [-9] => 'dumb'
    [-8] => 'dumb'
)

Yeah.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

Leave a Comment

{ 1 trackback }

Previous post:

Next post: