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 />'; } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Share Posted July 18, 2007 I don't really understand... Quote Link to comment 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. Quote Link to comment 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++; } ?> Quote Link to comment 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 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.