How to use array in Switch Case PHP
Â
Hello! I’ve been trying to use array in switch case but I’m confused, can anyone teach me how to combine this two, is this possible? Thanks
Â
Hello! I’ve been trying to use array in switch case but I’m confused, can anyone teach me how to combine this two, is this possible? Thanks
Â
Hi Stanley,
Â
First, you need to decide what do you want to do inside the looping of each array element and the case labels. Here is the basic construct.
Â
<?php
       $in = array(1,2,3,4,5); // fill the array with your case labels
       foreach ($in as $a) // loop for each element in
       {
               switch ($a)
               {
                       case 1:
                               echo '1 is in the list';
                               break;
                       case 2:
                               echo '2 is in the list';
                               break;
                       case 3:
                               echo '3 is not the list';
                               break;
                       case 4:
                               echo '4 is not the list';
                               break;
                       case 5:
                               echo '5 is not the list';
                               break;
                       default:
                               echo 'No Match Found<br />';
               }
       }
?>
Â
I hope it helped.