views:

101

answers:

3

I have some array:

array
  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?

+3  A: 

How about this?

foreach($fruits as $index => $fruit) {
    echo ($index+1).". ".$fruit."\n";
}
txwikinger
like this one, but maybe html is better :) thanks for the answer bro.
Adam Ramadhan
+5  A: 

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>
karim79
+1  A: 

If HTML

<ol>
<?php
foreach ($fruits as $fruit) {
    echo "<li>", $fruit;
    }
?>
</ol>
R. Hill
+1 for using `,` in `echo` and for omitting the `</li>`.
nikic
W3C: http://www.w3.org/TR/html401/struct/lists.html#h-10.2"LI ... Start tag: required, End tag: optional"
R. Hill
A personal preference. When using non-strict HTML, I use capital letters for elements (as lowercase tags have their origination with XHTML). But then again, I don't use non-strict HTML.
kingjeffrey