Solved

Switch Statement with More then One Variable

Posted on 2007-11-19
7
198 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
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
alternatively:


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

Open in new window

0
 
LVL 1

Expert Comment

by:TECHHEAD_biz
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Expert Comment

by:TECHHEAD_biz
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now