bluegray Posted July 18, 2010 Share Posted July 18, 2010 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 . Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/ Share on other sites More sharing options...
.josh Posted July 18, 2010 Share Posted July 18, 2010 in_array Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087865 Share on other sites More sharing options...
bluegray Posted July 18, 2010 Author Share Posted July 18, 2010 in_array Then I would have to do in_array once for every other kind of country there is. Not Very Efficient. Maybe it can be done with in_array but you need to specify how because the how is where I'm stuck. Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087876 Share on other sites More sharing options...
Pikachu2000 Posted July 18, 2010 Share Posted July 18, 2010 What is the structure of the array you have? Can you post some sample data, or perhaps a print_r() of it? Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087877 Share on other sites More sharing options...
bluegray Posted July 18, 2010 Author Share Posted July 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087881 Share on other sites More sharing options...
Pikachu2000 Posted July 18, 2010 Share Posted July 18, 2010 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087886 Share on other sites More sharing options...
bluegray Posted July 18, 2010 Author Share Posted July 18, 2010 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? Yes the values come from a Database query. Why, what are your thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087899 Share on other sites More sharing options...
AbraCadaver Posted July 18, 2010 Share Posted July 18, 2010 For fun: $non_us = (count(array_unique($country)) > 1); Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087903 Share on other sites More sharing options...
bluegray Posted July 18, 2010 Author Share Posted July 18, 2010 Thanks Kindly everyone! Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087908 Share on other sites More sharing options...
.josh Posted July 18, 2010 Share Posted July 18, 2010 Ah okay, I slightly misunderstood, I thought it was the other way around, checking if 'US' was in array. I like the array_unique solution. Here's another one-liner: if (count(array_diff($countries,array('US'))) > 0) { // non-US in array } Quote Link to comment https://forums.phpfreaks.com/topic/208111-checking-an-array-for-multiple-kinds-of-values/#findComment-1087918 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.