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

GibsonFX
  • 1,000
  • 2
  • 10
  • 33
  • 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? – Nigel Ren Feb 15 '23 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 – GibsonFX Feb 15 '23 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? – Nigel Ren Feb 15 '23 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? – GibsonFX Feb 15 '23 at 14:18
  • switch ($class[0]) { case 'middle': case 'onright': $parallaxDir = '0'; break; case 'top': case 'onleft': $parallaxDir = '1'; break; }; – GibsonFX Feb 15 '23 at 14:19
  • $class[0]||$class[1] is an array from a JSON decode as I said – GibsonFX Feb 15 '23 at 14:21
  • If it was simply a case of using a combination of the values, concatenate them and check that (`$class[0] . ',' . $class[1]` and `case '1,2':`. But this depends if that is reasonable, which depends on how it is used. – Nigel Ren Feb 15 '23 at 14:26

1 Answers1

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.

E-g
  • 524
  • 2
  • 12