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! Link to comment https://forums.phpfreaks.com/topic/60028-solved-like-foreach-but-i-only-want-it-happening-a-few-times/ 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/60028-solved-like-foreach-but-i-only-want-it-happening-a-few-times/#findComment-298552 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. } ?> Link to comment https://forums.phpfreaks.com/topic/60028-solved-like-foreach-but-i-only-want-it-happening-a-few-times/#findComment-298554 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/60028-solved-like-foreach-but-i-only-want-it-happening-a-few-times/#findComment-298558 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. Link to comment https://forums.phpfreaks.com/topic/60028-solved-like-foreach-but-i-only-want-it-happening-a-few-times/#findComment-298564 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.