Solved

php in_array multiple values

Posted on 2013-01-01
4
1,844 Views
Last Modified: 2013-01-01
Can you use in_array to see if an array contains multiple values.  Here is how I have been using it.

if (in_array("10527", $PayPalResult)) {
    $url_var = "card_number&card_type&";
}

Open in new window


how would I also check for the value 10510 in the same if statement?
0
Comment
Question by:Luey
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38735127
If you look at the documentation, it demonstrates passing an array as the "needle". Just create a new array with the corresponding values being sought.

e.g.

$to_find = array("10527", "10510");

if (in_array($to_find, $PayPalResult)) {
    $url_var = "card_number&card_type&";
}

Open in new window

0
 

Author Comment

by:Luey
ID: 38735226
@ kaufmed   I have tried your code and cannot make it work.  I have looked at the documentation and agree that it should work according to the documentation. I can use either number (10527, 10510) by themselves and it works perfect.  But in the array it does not.  Any suggestions.  I know it is not my other codes because I have stripped it down to just this and it still does not work.
0
 

Author Closing Comment

by:Luey
ID: 38735309
This does work the way I asked the question.  However I need it to work more like an "or".  I need to know if 10527 or 10510 is in the array I cannot make it work for that.
0
 

Author Comment

by:Luey
ID: 38735351
This made it work like I needed it to.  

if (in_array("10527", $PayPalResult) or in_array("10510", $PayPalResult)) {
    $url_var = "card_number&card_type&";
}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 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