I couldn't find a definitive answer on php.net so I'm hoping you can answer.

Can the case of a switch statement detect an array key rather than the value?

I tried a little experiment, see below, to see if I could get this to work but all I get is the default value returning.

$inString = "$_POST[latitudefield1]";

switch($_POST){
	case $_POST['latitudefield1']:
		echo"1";
		break;
	case "latitudefield1":
		echo"2";
		break;
	case "".$_POST['latitudefield1']."":
		echo"3";
		break;
	case $inString:
		echo"4";
		break;
	default:
		echo"5";
}

Am I attempting the impossible, or have I just got the wrong format for the case?

    It certainly can, but the problem is that you are checking if $_POST is equal to any of the possible cases. Obviously, it is not equal to any of them.

      Not entirely sure what you're asking... are you trying to see if a given index exists in the $_POST array?

      If so, the answer is yes it's possible, but perhaps there is a better way of going about doing this? Can you give us a little more information/explanation as to what you're doing?

        Thank you for the replies.

        bradgrafelman - yes I was trying to see if a given index exists in the $POST array. The reason being I have four latitude and longitude pairs the user inputs on my page (to create a working area). I was hoping to use a switch to go through the $POST array, rather than have a series of else if's as I thought the code would be neater/ easier to read.

        laserlight - I may have misinterpreted your reply but when you said it certainly can, did you mean I can get a case statement to evaluate the array key and not value?

        I tried another experiment to see what would work, but as you can see this only picks up the values.

        $arr = array(0=>'a',1=>'b',2=>'c',3=>'d');
        
        for($x=0;$x < sizeof($arr);$x++){
        	echo("x=".$x." ");
        	echo("value=".$arr[$x]." ");
        
        switch($arr[$x]){
        	case 'a':
        		echo" case a <br/>";
        		break;
        	case 'b':
        		echo" case b <br/>";
        		break;
        	case 2:
        		echo" case 2 <br/>";
        		break;
        	case 'd':
        		echo" case d <br/>";
        		break;
        	default:
        		echo" None of the above <br/>";
        		break;
        }
        }
        
        /* OUTPUTS
        x=0 value=a case a
        x=1 value=b case b
        x=2 value=c None of the above
        x=3 value=d case d 
        */
        

        I really appreciate all your help - Emma

          Emma wrote:

          I was trying to see if a given index exists in the $POST array.


          There are a few possible approaches to this. One way is to use [man]isset[/man] or [man]empty[/man] on the array element at the given index, e.g.,

          if (isset($_POST['latitudefield1']))

          Another way is to make use of [man]array_key_exists/man.

          Emma wrote:

          I may have misinterpreted your reply but when you said it certainly can, did you mean I can get a case statement to evaluate the array key and not value?

          Yes, I did. But this is because the array key here is just a string, so it is nothing more than putting a string in the case label. What you had in mind was passing the array as the switch expression, and then listing the keys in the case labels and hoping that a key would match, but a switch simply does not work in that way.

            laserlight;10945504 wrote:

            What you had in mind was passing the array as the switch expression, and then listing the keys in the case labels and hoping that a key would match, but a switch simply does not work in that way.

            It was yes, it would be nice if switch did work that way but never mind 🙁 Thanks for your reply.

            Also despite bradgrafelman's sig, I can't find the thread tools with which to mark this resolved - sorry.

              You can find the thread tools link on a menu bar towards the top right of this page.

                Yes sorry just spotted them, you can't see that menu bar when you press the 'Go advanced' button on reply to thread.

                Also forgot to say thanks for the array_key_exists functions looks handy.

                  Thought I'd just add this, in case anyone finds it useful.

                  foreach ($_POST as $postKey => $postValue) {
                  	echo("pk= ".$postKey." ");
                  	switch($postKey){
                  		case 'latitudefield1':
                  			echo"lat point1 <br />";
                  			break;
                  		case 'latitudefield2':
                  			echo"lat point2 <br />";
                  		default:
                  			echo"nope <br />";
                  	}		
                  }
                  
                  /* OUTPUTS
                  pk= latitudefield1 lat point1 
                  pk= longitudefield1 nope 
                  pk= latitudefield2 lat point2
                  pk= longitudefield2 nope
                  */
                  

                  This uses the array key as the case but not quite as I originally imagined.

                    6 years later

                    test.php?test <- returns "hello"

                    switch($_GET){
                    	case ($_GET['test'])==='':
                    		echo "hello";
                    	break;
                    	default:
                    	echo "nope";
                    }
                    

                    test.php?test=test <- returns "hello"

                    switch($_GET){
                    	case ($_GET['test'])=='test':
                    		echo "hello";
                    	break;
                    	default:
                    	echo "nope";
                    }
                    

                      This will work with or without a set value.

                      test.php?test or test.php?test=test both echo "hello"
                      test.php?negative echo "nope"

                      switch($_GET){
                      	case (isset($_GET['test']))===true:
                      		echo "hello";
                      	break;
                      	default:
                      	echo "nope";
                      }

                        No, don't use a switch for this. It is simply not the right tool for the job. An if/else chain will do.

                          Write a Reply...