Jump to content

Checking an array for multiple kinds of values...


bluegray

Recommended Posts

Forgive my brevity, I'm short on time, and don't exactly know how to articulate this question.

 

So I'm going to have an array with different country names in it.  Call it $country []. 

 

Now I need to check the array for any non-U.S. country.  The array will always have at least 1 U.S. in it, but I need to know how to check for true/false the presence of there also being something else like UK, MX, AU, etc. 

 

Do I need to specify anything else?  Please help me, PHP addicts  :confused:.

 

Link to comment
Share on other sites

What is the structure of the array you have? Can you post some sample data, or perhaps a print_r() of it?

 

Okay, so here's my goal.  I need to make sure a foreign country portion of the page is not displaying if there are no foreign countries (native country being U.S.) in the array.

 

here's an example of the $country array on a dump:

array(4) { 
[0]=>  string(2) "US" 
[1]=>  string(2) "US" 
[2]=>  string(2) "US"  
} 

 

So naturally I don't want the "Foreign" country section to display if there are no foreign countries.  However, there are alternatively some 40+ other country names to check for so I need a solution that scans for anything that's not the U.S. and then returns a true/false. 

 

Link to comment
Share on other sites

Try this. If $non_us == TRUE, that means there are other countries in the array. Do these values come from a database query, by chance?

 

$non_us = '';
foreach( $array as $v ) {
     if( $v != 'US' ) {
          $non_us = TRUE
          break;
     }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.