SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Inspecting k=>v pairs for nils
-
Nov 23, 2004, 13:35 #1
Inspecting k=>v pairs for nils
I have a variable that holds an array. What I need to do it check if all the variables equal a particular value. What's the easiest way to do this.
For example, in the namespace, I have this floating around,
Code:$ThisVar = array('K1' => 'V1', 'K2' => 'V2', etc, etc);
Code:if (($ThisVar['K1'] == '') && ($ThisVar['K2'] == '') && ($ThisVar['K3'] == '') && ($ThisVar['K4'] == '') && ($ThisVar['K5'] == '') && ($ThisVar['K6'] == '') && ($ThisVar['K7'] == '') && ($ThisVar['K8'] == '') && ($ThisVar['K9'] == '') && ($ThisVar['K10'] == '')) { ...do this... } else { ...do that... }
Thanks,
John
-
Nov 23, 2004, 17:24 #2
- Join Date
- Nov 2004
- Location
- Parry Sound, ON
- Posts
- 725
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:foreach($ThisVar as $k => $v)
{
if($v)
{
//do this
}else
{
// do that
}
}
-
Nov 23, 2004, 17:32 #3
- Join Date
- Nov 2004
- Location
- Parry Sound, ON
- Posts
- 725
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Er...just reread your question. You probably want more like this:
PHP Code:$flag = 0;
foreach($ThisVar as $k => $v)
{
if($v){$flag = 1;break;}
}
if($flag){ /*do this*/ }else{ /*do that*/ }
-
Nov 23, 2004, 19:17 #4
What I need it to do is check them all...but only do an "if" if all of the $v's equal ''. If even one $v != '' then I don't need the if to happen.
-
Nov 24, 2004, 19:46 #5
- Join Date
- Nov 2004
- Location
- Parry Sound, ON
- Posts
- 725
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, you're in luck, cause that's what the second code should do. Try testing if(!$flag) or switch "do this" with "do that"...I'm not sure exactly what you need.
Bookmarks