5

How can i make this code work? TY!

$site = '1'

    $mysites = array('1', '2', '3', '4', '5', '6');
            foreach($mysites as $mysite) 
            {
            echo $mysites;  **but not the site with value 1**
            }

4 Answers 4

13

A simple if will suffice:

$site = '1';

$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite) 
{
    if ( $mysite !== '1' )
    {
        echo $mysite;
    }
}

or if you wan't to check against the $site variable:

$site = '1';

$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite) 
{
    if ( $mysite !== $site )
    {
        echo $mysite;
    }
}
2
  • Anyway, $site = '1' isn't valid, $site = '1'; is. Plus, echo $mysites; won't display anything since $mysites is an array. Please, don't copy/paste bad code from the OP question just to be faster to answer.
    – Shikiryu
    Commented Dec 23, 2010 at 10:06
  • @ClemDesm I've fixed those errors. It wasn't my intention to answer as quickly as possible, I just completely missed those errors. Thanks for pointing them out! Commented Dec 23, 2010 at 10:11
6
$site = '1'

    $mysites = array('1', '2', '3', '4', '5', '6');

    foreach($mysites as $mysite) {
        if ($mysite == $site) { continue; }

        // ...your code here...
    }
2
  • 1
    well this is a very small and simple piece of code, which directly answers the question asked, and such a simple code is pretty much self explanatory... If not, would you advice me for the future what would be a suitable explanation for this one. Commented Feb 18, 2014 at 15:19
  • a small comment would be very helpful :) but who cares.. you are right. i apology.
    – Alex Tape
    Commented Feb 18, 2014 at 21:28
3

Just use an if statement:

foreach($mysites as $mysite) {
    if ($mysite !== $site) {
        echo $mysite;
    }
}
0

you can do this:

$mysites = array('1', '2', '3', '4', '5', '6');
        foreach($mysites as $mysite) 
        {
if($mysite != 1){
        echo $mysite;
}
        }
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.