Solved

php in_array multiple values

Posted on 2013-01-01
4
1,784 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to count occurrences of each item in an array.

785 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