Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I loop through an array? 1

Status
Not open for further replies.

casabella

Programmer
Aug 30, 2007
59
US
I wonder if I set a simple variable like

$var = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

how to treat it as an array and loop through it to create a link for each letter of the alphabet?

Regards,


Jose
 
OK, I figured it out. Possible not the most elegant solution for a solution non then less ...

Code:
<?
$x = 0;
$alphabet = array("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
while ($x < 26) {
echo ':' . $x . ':' . $alphabet[0][$x] . '<br />'; $x++;
}
?>

Regards,

Jose
 
weeellll..... you could use a little known feature in php and use some curly braces

FOr any string of letters

Code:
$var = 'abcdefghijklmnop';
for ($i=0; $i<strlen($var); $i++){
 echo $var{$i};
 echo "<br/>";
}

or you could do something equally clever if you wanted to cover the whole alphabet and can't be bothered to plot out the letters yourself.

Code:
$start = 'a';
$end = 'z';
for ($i=ord($start); $i<=ord($end); $i++){
	$letters[] = chr($i);
	$letters[] = strtoupper(chr($i));
}
echo "<pre>".print_r($letters, true)."</pre>";

 
jpadie,

Very slick jpadie, very slick!

Have you ever considered putting together a quick reference guide or reference book? Given the wealth of tips you seems to have in your toolbox, i would encourage you to.

Thanks!


Jose
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top