How to apply limit into foreach loop in PHP

Home Forums PHP How to apply limit into foreach loop in PHP

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1394
    Mofizul
    Keymaster

    Suppose the variable $posts is containing 20 posts. I want to display only 4 posts. How to I can do that?

    #1395
    Mofizul
    Keymaster

    Write your code like

    foreach (array_slice($posts, 0, 4) as $post){
    echo $post['title'];
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.