Jump to content

[SOLVED] Like foreach, but I only want it happening a few times.


jlange

Recommended Posts

Maybe its something really simple, but I'd like to get a foreach going for items in an array, but I only want it happening a set number of times. I'm not really that much of an expert on loops (obviously), but I understand PHP, so go easy on me but not too easy!

Link to comment
Share on other sites

You could do something like:

 

<?php

$count = 0;
foreach ($value as &$val) {
    //code
    if ($count >= 5) {
        break;
    } else {
        $count++;
    }
}

?>

 

But if you don't need to use foreach:

 

<?php
for($i=0;$i<5;$i++){
    print $array[$i]."<br>";
}
?>

Link to comment
Share on other sites

the foreach is the best option still..i'm using simplepie (rss parser) and it does some weird OOP things (i think) which are way over my head. thanks for the reply, though, gives me exactly the answer I need.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.