douye1940 2014-12-13 13:07
浏览 94
已采纳

如何从php数组开始使用索引1的项进行循环?

I would like to know, how to start looping of a php array with the second item.

  • 写回答

1条回答 默认 最新

  • duan0403788996 2014-12-13 13:14
    关注

    Instead of the standard for loop to go through an array, for example:

    for($i = 0; $i < count($array); $i++) {
        echo $array[$i] . " ";
    }
    

    Outputs "one two three four five".

    Just set $i equal to 1 instead, like so:

    for($i = 1; $i < count($array); $i++) {
        echo $array[$i] . " ";
    }
    

    Now it outputs this: "two three four five".

    However, if you wanted to get the last four items of an array, you can use array_slice().

    $sliced = array_slice($array, -4);
    
    for($i = 0; $i < count($sliced); $i++) {
        echo $sliced[$i] . " ";
    }
    

    Outputs "two three four five".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容