0

My question is the opposite of this Using two values for one switch case statement

I'd like to use multiple values in the switch part of the statement, not the case part which I already know how to do.

<?php
$three = json_decode( $ex[ 'three-images' ] );
$i = 0;
$imgs = $trans = $class = $parallaxDir = [];
foreach ( $three as $img ) {
    $imgs[ $i ] = $img->Image;
    $parallax[ $i ] = $img->Transition;
    $class[ $i ] = $img->Class;
    //for ($ii = 0; $ii < 3; $ii++) {
    switch ( $class[ $i ] ) {
        case 'middle':
        case 'onright':
            $parallaxDir[ $i ] = '0';
            break;
        case 'top':
        case 'onleft':
            $parallaxDir[ $i ] = '1';
            break;
        default:
            $parallaxDir[ $i ] = '9';
            break;
    };
    //}     
    $i++;
}
?>
<div class="side image <?php echo $class[1]; ?>" <?php if(!empty($parallax[1])) { echo 'data-para="' . $parallax[1] . '"'; } ?> <?php if(!empty($parallaxMob)) { echo 'data-para-mobile="' . $parallaxMob . '"'; } ?> <?php if(!empty($parallaxDir[1])) { echo 'data-para-dir="' . $parallaxDir[1] . '"'; } ?>> 
    <img class="parallax" src="<?php echo $imgs[1]; ?>" alt="<?php echo explode('[',trim($item->title)[0]); ?> image"/> 
</div>

This is the full code, seems it was working fine as I had it but for some reason when trying to implement it later down the line the array values were returning as empty even though they weren't.

So once I took the [1] off the parallaxDir if(!empty) check it started working

7
  • Can you explain how you would like it to operate? You have 2 values in this case, how does this like to the case statements you have? Commented Feb 15, 2023 at 14:12
  • The CMS gives the class a text value, the text value will be uniquie per image and is combined with a lot of other info in a decoded JSON string so each one has to be pulled out via the Array, then depending on what text string is returned it the passed the value to the PHP variable which is then used to combine with a data-attribute, so the above is a simplifed version Commented Feb 15, 2023 at 14:14
  • So how does that relate to the code you show? How does the combination of $class[0]||$class[1] relate to the case statements? Commented Feb 15, 2023 at 14:16
  • I don't get what you're asking, I'm asking if it's possible to use multiple inputs in the switch part of the statement, why do you need to know anything else? Commented Feb 15, 2023 at 14:18
  • switch ($class[0]) { case 'middle': case 'onright': $parallaxDir = '0'; break; case 'top': case 'onleft': $parallaxDir = '1'; break; }; Commented Feb 15, 2023 at 14:19

1 Answer 1

1

No, not possible.

Though you could do something like this:

foreach([$class[0], $class[1]] as $value) {
      switch($value) {
           // case...
      }
}

But you are probably better off using if statements.

Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't seem to work
Use break 2; to also break the loop instead of simply break;
It's returning a value now but only the last value in the sequence
Show me your code
Posted the full code but it's working now, seems that there was some issue with the if(!empty) check on the Array value was returning as false even though it did contain a value.

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.