Tools, FAQ, Tutorials:
Creating an Array with a Sequence in PHP
How To Create an Array with a Sequence of Integers or Characters in PHP?
✍: FYIcenter.com
The quickest way to create an array with a sequence of integers or characters is to use the range() function. It returns an array with values starting with the first integer or character, and ending with the second integer or character. Here is a PHP script on how to use range():
<?php print("Integers:\n"); $integers = range(1, 20, 3); print_r($integers); print("\n"); print("Characters:\n"); $characters = range("X", "c"); print_r($characters); print("\n"); ?>
This script will print:
Integers: Array ( [0] => 1 [1] => 4 [2] => 7 [3] => 10 [4] => 13 [5] => 16 [6] => 19 ) Characters: Array ( [0] => X [1] => Y [2] => Z [3] => [ [4] => \ [5] => ] [6] => ^ [7] => _ [8] => ` [9] => a [10] => b [11] => c )
Of course, you can create an array with a sequence of integers or characters using a loop. But range() is much easier and quicker to use.
⇒ Padding an Array with a Given Value in PHP
⇐ Looping through an Array without "foreach" in PHP
2023-07-28, 4398🔥, 1💬
Popular Posts:
How to login to the Developer Portal internally by you as the publisher? Normally, the Developer Por...
How to create the Hello-3.0.epub package? I have all required files to create Hello-3.0.epub. To cre...
How To Change Text Fonts for Some Parts of a Paragraph? If you want to change text fonts or colors f...
How to use "{{...}}" Liquid Codes in "set-body" Policy Statement? The "{{...}}" Liquid Codes in "set...
How To Set session.gc_divisor Properly in PHP? As you know that session.gc_divisor is the frequency ...