0

I'm trying to display results in a foreach loop.

I have an array like so:

$slides = array(
    1 => $params->get('param_1'), 
    2 => $params->get('param_2'),
    3 => $params->get('param_3')
);

If a parameter in the backend is set to yes, the value equals 1, and thus a result is displayed.

So what I'm trying to write is, foreach array value that is equal to 1

I'm aware that I can't use something like this:

foreach (slides == 1) { 
    // echo stuff here
}

How would I write this foreach loop? Any help or guidelines would be highly appreciated

1
  • The issue here is not the foreach loop at all. You want to iterate over each element. The issue is to test the value of each item in the array and only if it answers a certain condition, then you output it. Commented Sep 8, 2013 at 22:02

1 Answer 1

3
foreach ( $slides as $slide ) { 
    if ( $slide != 1 ) continue;
    // echo stuff here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Joseph, this works perfectly. I'll accept when it allows me to

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.