Solved

Switch Statement with More then One Variable

Posted on 2007-11-19
7
205 Views
Last Modified: 2013-12-13
This is an example:

Lets say I have 1000 urls

http://www.abc.com/var1=1&var2=1
http://www.abc.com/var1=1&var2=2
http://www.abc.com/var1=1&var2=3
http://www.abc.com/var1=1&var2=4
http://www.abc.com/var1=1&var2=5
http://www.abc.com/var1=2&var2=1
http://www.abc.com/var1=2&var2=2
http://www.abc.com/var1=2&var2=3
http://www.abc.com/var1=2&var2=4
http://www.abc.com/var1=2&var2=5

This is basically the pattern.  Let's say I want to specify the url that has var1=2 and var2=5.  How would you write that in the most efficient way?  Thanks.
0
Comment
Question by:smoothcat11
7 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 20316958
switch ($var1){

              case "1":

                        switch ($var2){

                                   case "1":
                                                URL
                                         break;
 
                                   case "2":
                                               URL
                                          break;
                                                }

                           case "2"

                                       switch ($var2){

                                           case "1":
                                                    URL
                                              break;

                                           case "2":
                                                    URL
                                               break;
                                                              }
                                }

repeat the process as needed
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20317831
alternatively:


switch ( "$var1:var")
{
  case "2:5":
     ....
     break; 
 
}

Open in new window

0
 
LVL 1

Expert Comment

by:TECHHEAD_biz
ID: 20317986
More simply, you could use an associative array of associative arrays.  I would need a refresher on the semantics of PHP, but this is how I would do it in any other language.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:smoothcat11
ID: 20317998
Can anyone give me an example of an associative array for this situation?
0
 
LVL 1

Expert Comment

by:TECHHEAD_biz
ID: 20318014
It would be something along these lines.  Again, I am probably wrong on the semantics.

$vars = {};
$vars['1'] = {'1' => 'someURL', '2' => 'someOtherURL'};
$vars['1']['2'] == 'someOtherURL';
0
 
LVL 5

Expert Comment

by:palanee83
ID: 20318856
Yes you can do this by using associative array like below
<?php
 
$varURL = array(1=>array("http://www.abc.com/var1=1&var2=1","http://www.abc.com/var1=1&var2=2","http://www.abc.com/var1=1&var2=5"),
2=>array("http://www.abc.com/var1=2&var2=1","http://www.abc.com/var1=2&var2=2","http://www.abc.com/var1=2&var2=5"));
foreach($varURL as $key=>$URLS)
{
	for($j=0;$j<count($URLS);$j++)
	{
 
		if($URLS[$j]=="http://www.abc.com/var1=2&var2=5")
		{
 
			echo "URL Found: ".$URLS[$j];
			exit;
		}
	}
}
?>

Open in new window

0
 

Author Closing Comment

by:smoothcat11
ID: 31410048
I really like this solution.  don't forget that if you use just a 2 you need to make it
"2:" not "2" for the catch all
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
push Book Today button to right 10 39
MSSQL - Lock Row from reading by other programs 9 40
WordPress 8 38
Programming Language for Wordpress 7 43
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question