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

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

WBOY
Release: 2016-06-23 14:08:22
Original
810 people have browsed it

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



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


回复讨论(解决方案)

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

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

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


  • }
    ?>

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

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

  • $i++;
    }
    ?>

    可以用html的ol有序标签,

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

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

    聪明写法

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

    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template