in_array as a case in a switch statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
2jayphp
Forum Newbie
Posts: 2
Joined: Tue Dec 16, 2008 12:30 pm

in_array as a case in a switch statement

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: in_array as a case in a switch statement

Post 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?
2jayphp
Forum Newbie
Posts: 2
Joined: Tue Dec 16, 2008 12:30 pm

Re: in_array as a case in a switch statement

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: in_array as a case in a switch statement

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: in_array as a case in a switch statement

Post 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';
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply