PHP foreach() return only last 50 items

后端 未结 3 985
名媛妹妹
名媛妹妹 2021-01-25 04:08

I am currently using the following PHP code to return and format output from an .htm file:

\";
$lines = file(\"http://www.example.c         


        
相关标签:
3条回答
  • 2021-01-25 04:22

    You're looking for the array_slice function:

    array_slice() returns the sequence of elements from the array array as specified by the offset...

    $lines = file(...);
    $lines = array_slice($lines, -50);
    
    0 讨论(0)
  • 2021-01-25 04:39

    Use array_slice() to slice off the last 50 elements of $lines prior to running the foreach() { } loop.

    $lines = array_slice($lines, -50);
    
    0 讨论(0)
  • 2021-01-25 04:41

    Another alternative is to do the calculation your own and skip all but the X last ones:

    $last  = 50;
    $count = count($lines);    
    foreach ($lines as $line) 
    {
        if ($count-- > $last) continue;
        ...
    
    0 讨论(0)
提交回复
热议问题