Page 1 of 1

in_array as a case in a switch statement

Posted: Tue Dec 16, 2008 12:33 pm
by 2jayphp
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hey folks, first post here as I'm running into some new issues during my development as of late.

so thanks in advance for taking a look 8)


Basically, i have a switch statement that is looking for an ID #.

I have a certain scenario where the result will be the same for multiple ID#'s.

Can i create an array of these ID's and use it in a single case?

For example:

Code: Select all

$id_array = array(7,8,9,23);
 
Switch (ID){
 
   case 1:
            do stuff;
            break;
   case 2:
            do stuff;
            break;
   case in_array($id_array):
            do stuff;
            break;
}
Thanks again! :)


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: in_array as a case in a switch statement

Posted: Tue Dec 16, 2008 12:36 pm
by John Cartwright
I'm not exactly sure what you are trying to do. The simple answer is no, but there is probably a better way to do this anyway.
Can you explain a bit furthur?

Re: in_array as a case in a switch statement

Posted: Tue Dec 16, 2008 12:50 pm
by 2jayphp
thanks for the quick response!

I'm using a CMS which uses different ID#'s to differentiate between objects.

Im writing a small function that returns a custom tag i will eventually need later.
This tag is built based on the ID# of the current page you're on.
(gosh i hope that makes sense)

so rather than do 3 seperate cases for the same return ("subpage")

Code: Select all

 
   switch ($ID_number){
         case 1:
            return "first";
            break;         
         case 2:
            return "second";
            break;         
         case 3:
            return "third";
            break;
         case 4:
            return "subpage";
            break;
         case 5:
            return "subpage";
            break;
         case 6:
            return "subpage";
            break;
}
 
I wanted to do a single case on an array of those #'s

so

Code: Select all

$id_array = array(3,4,5)
switch ($ID_number){
         case 1:
            return "first";
            break;         
         case 2:
            return "second";
            break;         
         case 3:
            return "third";
            break;
        case in_array($id_array);
            return "subpage"
            break;
 

As you mentioned, im sure there might even be a much better way to do this! In which 'case' ( :P ) I'm all ears.

Re: in_array as a case in a switch statement

Posted: Tue Dec 16, 2008 12:55 pm
by John Cartwright

Code: Select all

$id_array = array(3,4,5)
 
switch($id) {
   case 1:
      return "first";
   break;        
   case 2:
      return "second";
   break;        
   case 3:
      return "third";
   break;
   default :
      if (in_array($id, $id_array)) {
         return "subpage"
      }
      return false;
}
 
Not neccesarily better, but you could use the default case for any other values that were not previously captured, and check whether they are in the array there.

Re: in_array as a case in a switch statement

Posted: Tue Dec 16, 2008 4:54 pm
by pickle
Or just don't break out of certain cases:

Code: Select all

switch($id)
{
    case 1:
        echo 'one';
        break;
    case 3:
        echo 'three';
        break;
    case 4:
    case 6:
    case 8:
        echo 'even';
        break;
    default:
        echo 'unknown';
}