SharkBait Posted April 11, 2006 Share Posted April 11, 2006 My brain is mush.. looking for some insight into my foreach loopTrying to figure out how to skip over things in an Foreach loop via a conditional.[code]<?php$UserA = 1;$UserB = 2;$UserC = 4;$array = array("Blah" => "goober", "Blah2" => "goober2", "Blah3" => "goober3",);foreach($array as $item => $desc) { // I want to skip item 1 and 3 if $UserB == 2 echo "<p>{$item} -> {$desc} </p>";}?>[/code]I though I could use next($array); but that wasnt it, then I thought about using break(); but well.. that breaks it :)I'm missing something simple I think.... any help appreciated. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 11, 2006 Share Posted April 11, 2006 Try something like:[code]<?php$UserA = 1;$UserB = 2;$UserC = 4;$array = array("Blah" => "goober", "Blah2" => "goober2", "Blah3" => "goober3",);foreach($array as $item => $desc) { // I want to skip item 1 and 3 if $UserB == 2 if ($UserB == 2 && ($item == "Blah" || $item == "Blah3")) continue; else echo "<p>{$item} -> {$desc} </p>";}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
SharkBait Posted April 11, 2006 Author Share Posted April 11, 2006 [!--quoteo(post=363807:date=Apr 11 2006, 02:37 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 11 2006, 02:37 PM) [snapback]363807[/snapback][/div][div class=\'quotemain\'][!--quotec--]Try something like:[code]<?php$UserA = 1;$UserB = 2;$UserC = 4;$array = array("Blah" => "goober", "Blah2" => "goober2", "Blah3" => "goober3",);foreach($array as $item => $desc) { // I want to skip item 1 and 3 if $UserB == 2 if ($UserB == 2 && ($item == "Blah" || $item == "Blah3")) continue; else echo "<p>{$item} -> {$desc} </p>";}?>[/code]Ken[/quote]Though, will the above still print out Blah2 ??What I am trying to do is if it is a certain user logged in, I dont want them to see certain links that the foreach returns. So Admin sees Blah, Blah2, Blah3, but Lacky only seens Blah2 and not Blah or Blah3 Quote Link to comment Share on other sites More sharing options...
SharkBait Posted April 11, 2006 Author Share Posted April 11, 2006 Alright figured it out, just had to run through it outloud a couple of times to get my conditional the way I wanted it.[code]<?phpforeach ($ADMINLINKS as $HEADER => $LINKS) {if(is_array($LINKS)) { if(($USERLEVEL == $SALES) && ($HEADER != "Users")) continue; ?> /* blahblahblah do code here */<?php}}?>[/code]Thanx for the 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.