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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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!
6
$site = '1'

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

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

        // ...your code here...
    }

2 Comments

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.
a small comment would be very helpful :) but who cares.. you are right. i apology.
3

Just use an if statement:

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

Comments

0

you can do this:

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

2 Comments

it was !== this is was the problem and it fixed now
not the same result you can watch this link ibb.co/PxYBBsT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.