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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.