Solved

# Switch Statement with More then One Variable

Posted on 2007-11-19
206 Views
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
Question by:smoothcat11
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 28

Expert Comment

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

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

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

}
``````
0

LVL 1

Expert Comment

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

Author Comment

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

LVL 1

Expert Comment

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

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;
}
}
}
?>
``````
0

Author Closing Comment

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

Question has a verified solution.

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

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â€¦
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
###### Suggested Courses
Course of the Month6 days, 3 hours left to enroll