PHP Arrays, displaying fruits like 1.apple 2.banana, etc
I have some array:
arra开发者_开发百科y
0 => string 'Apple'
1 => string 'banana'
2 => string 'Nanas'
3 => string 'Grape'
What is the best way to display
1. Apple 2. banana 3. Nanas 4. Grape
Any Ideas?
If the output you are after is HTML, then an ordered list makes sense:
<ol>
<?php foreach($fruits as $fruit) { ?>
<li><?php echo $fruit; ?></li>
<?php } ?>
</ol>
How about this?
foreach($fruits as $index => $fruit) {
echo ($index+1).". ".$fruit."\n";
}
If HTML
<ol>
<?php
foreach ($fruits as $fruit) {
echo "<li>", $fruit;
}
?>
</ol>
精彩评论