开发者

PHP Arrays, displaying fruits like 1.apple 2.banana, etc

开发者 https://www.devze.com 2023-01-08 04:19 出处:网络
I have some array: arra开发者_开发百科y 0 => string \'Apple\' 1 => string \'banana\' 2 => string \'Nanas\'

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号