How to use array in Switch Case PHP

Asked By 10 points N/A Posted on -
qa-featured

 

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

SHARE
Answered By 5 points N/A #105844

How to use array in Switch Case PHP

qa-featured

 

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.

Related Questions