Home >Backend Development >PHP Tutorial >刚接触PHP,谁帮忙解决个问题

刚接触PHP,谁帮忙解决个问题

WBOY
WBOYOriginal
2016-06-23 14:08:22837browse

<?phpforeach ($xg as $key => $value){?><li><?php echo $value?></li><?php}?>



怎么在这个 的前面,自动加上从1开始的顺序数字。 


回复讨论(解决方案)

如果$xg 是从0索引连续的,可以这样:

<?phpforeach ($xg as $key => $value){?><li><?php echo ($key+1). $value?></li><?php}?>

$i = 0;
foreach ($xg as $key => $value){
$i++;
?>


  • }
    ?>

      <?phpforeach ($xg as $key => $value){?><li><?php echo $value?></li><?php}?>

      <?phpforeach ($xg as $key => $value){?><li><?php echo $value?></li><?php}?>
    嗯,这个方法不错。
    也可以这样子写:
    $i=1;
    foreach ($xg as $key => $value){
    ?>


  • $i++;
    }
    ?>

    可以用html的ol有序标签,

      <?phpforeach ($xg as $key => $value){?><li><?php echo $value?></li><?php}?>

      <?phpforeach ($xg as $key => $value){?><li><?php echo $value?></li><?php}?>

    聪明写法

    3楼的代码简洁啊,哈~~~

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn