simon551 Posted July 18, 2007 Share Posted July 18, 2007 Hi, I'm trying to use an array to build a dynamic menu. I use array_unshift to build up an array but I want to limit the number of items that can be returned to the first 4 in the array: $options=array(); if ($condition) { array_push($options,'a'); } if ($condition2) { array_unshift($options,'b'); } if ($condition3) { array_unshift($options,'c'); } if ($condition4) { array_unshift($options,'d'); } if ($condition5) { array_unshift($options,'e'); } foreach ($options as $key => $check) { echo $check.'<br />'; } Link to comment https://forums.phpfreaks.com/topic/60582-solved-array-newbie-limit-number-of-outputs/ Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Share Posted July 18, 2007 I don't really understand... Link to comment https://forums.phpfreaks.com/topic/60582-solved-array-newbie-limit-number-of-outputs/#findComment-301385 Share on other sites More sharing options...
simon551 Posted July 18, 2007 Author Share Posted July 18, 2007 it's possible that the array could have more than 4 items in it after all the options are gone through. I'm using foreach but I should probably be using something else... I want to just echo the first 4 items in the array. Link to comment https://forums.phpfreaks.com/topic/60582-solved-array-newbie-limit-number-of-outputs/#findComment-301392 Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Share Posted July 18, 2007 <?php $options=array(); if ($condition) { array_push($options,'a'); } if ($condition2) { array_unshift($options,'b'); } if ($condition3) { array_unshift($options,'c'); } if ($condition4) { array_unshift($options,'d'); } if ($condition5) { array_unshift($options,'e'); } $i = 0; foreach ($options as $key => $check) { echo $check.'<br />'; if($i > 3){ break; } $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/60582-solved-array-newbie-limit-number-of-outputs/#findComment-301398 Share on other sites More sharing options...
simon551 Posted July 18, 2007 Author Share Posted July 18, 2007 that didn't quite work. but this did $i = 0; foreach ($options as $key => $check) { $i++; echo $check.'<br />'; if($i > 3){ break; } } Thanks very much for your help Link to comment https://forums.phpfreaks.com/topic/60582-solved-array-newbie-limit-number-of-outputs/#findComment-301401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.