jlange Posted July 15, 2007 Share Posted July 15, 2007 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! Quote Link to comment Share on other sites More sharing options...
infid3l Posted July 15, 2007 Share Posted July 15, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
dbillings Posted July 15, 2007 Share Posted July 15, 2007 Or.... <?php // This will loop through the code however many times var duration is set. $duration = 10; for($i=0, $i < $duration, $i++){ // Code to execute. } ?> Quote Link to comment Share on other sites More sharing options...
drewbee Posted July 15, 2007 Share Posted July 15, 2007 OR .... <? // Where in the array to you want to start $counter = 0; while ($counter < count($array)) { echo $array[$counter]; $counter++; } ?> Quote Link to comment Share on other sites More sharing options...
jlange Posted July 15, 2007 Author Share Posted July 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.