Jump to content


Photo

Foreach Loop skip over


  • Please log in to reply
3 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 11 April 2006 - 09:18 PM

My brain is mush.. looking for some insight into my foreach loop

Trying to figure out how to skip over things in an Foreach loop via a conditional.

<?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>";
}
?>

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.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 09:37 PM

Try something like:
<?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>";
}
?>

Ken

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 11 April 2006 - 10:16 PM

[!--quoteo(post=363807:date=Apr 11 2006, 02:37 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 11 2006, 02:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try something like:
<?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>";
}
?>

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


#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 11 April 2006 - 10:53 PM

Alright figured it out, just had to run through it outloud a couple of times to get my conditional the way I wanted it.

<?php

foreach ($ADMINLINKS as $HEADER => $LINKS) {
if(is_array($LINKS)) {
  if(($USERLEVEL == $SALES) && ($HEADER != "Users")) continue;
  ?>
   /* blahblahblah do code here */
<?php
}
}

?>

Thanx for the help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users