PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How can I loop through the members of an array?


If you want to create a quick and easy loop to show the values of an array, then all you need is the trusty foreach control structure, thus:

<?php
$alphabet 
range("A","Z");
foreach(
$alphabet as $letter) { echo "Ooh look!  The letter $letter\n"; }
?>


Prints this:

Ooh look! The letter A
Ooh look! The letter B
Ooh look! The letter C
Ooh look! The letter D
Ooh look! The letter E
Ooh look! The letter F
Ooh look! The letter G
Ooh look! The letter H
Ooh look! The letter I
Ooh look! The letter J
Ooh look! The letter K
Ooh look! The letter L
Ooh look! The letter M
Ooh look! The letter N
Ooh look! The letter O
Ooh look! The letter P
Ooh look! The letter Q
Ooh look! The letter R
Ooh look! The letter S
Ooh look! The letter T
Ooh look! The letter U
Ooh look! The letter V
Ooh look! The letter W
Ooh look! The letter X
Ooh look! The letter Y
Ooh look! The letter Z



More arrays PHP Questions

How do I check if a variable is of array type?
How do I sort alphanumeric array data correctly?
How do I pick a random value from an array?
How do I remove and view the last element of an array?
How can I reverse sort an array keeping the correlation between the index and value?