Jump to content

Foreach Loop skip over


SharkBait

Recommended Posts

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.

[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.
Link to comment
https://forums.phpfreaks.com/topic/7141-foreach-loop-skip-over/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/7141-foreach-loop-skip-over/#findComment-25995
Share on other sites

[!--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
Link to comment
https://forums.phpfreaks.com/topic/7141-foreach-loop-skip-over/#findComment-26007
Share on other sites

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]
<?php

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

?>
[/code]

Thanx for the help!
Link to comment
https://forums.phpfreaks.com/topic/7141-foreach-loop-skip-over/#findComment-26015
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.