tags:

views:

60

answers:

4

I am processing a form and in turn, receiving a response code based on the information submitted. I have a list of approximately 40 response codes (and their meaning) in my hands and am trying to build an 'if' statement that checks against a predefined array and returns a specific value.

Just not sure how to do this

First pass conceptually:

$bads = array (1,2,3,4,5,6)


if ($output['responsecode'] == (any value in $bads) {
echo "you suck";
}

EDIT - Still receiving errors

I am using the following code:

$bad_resp1 = array("D","M","A","B","W","Z","P","L","N","C","U","G","I","R","E","S","0","O","B");
$bad_resp2 = array("N","P","S","U");
$bad_resp3 = array("200","201","202","203","204","220","221","222","223","224","225","250","261","262","263","264","300","400","410","411","420","421","430","440","441","460","461"); 

then calling the 'if' statement:

if (in_array($output['response1'], $bad_resp1) || in_array($output['response2'], $bad_resp2) || in_array($output['response3'], $bad_resp3)) {
            Header("Location: fail.php");
        }

I get the following error(s):

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\site\xyz.php on line 362

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\site\xyz.php on line 362

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\site\xyz.php on line 362

+8  A: 

Use in_array()

if (in_array($output['responsecode'], $bads)) { echo "you suck"; }
Coronatus
running this code, I receive the following error: Warning: in_array() expects parameter 2 to be array, null given in
JM4
code does not work as planned. See error in edit
JM4
A: 

As others have suggested, you can do:

if (in_array($output['responsecode'], $bads))
    ....

However, this is more efficient because it does not require a traversal of the $bads array:

$bads = array (1 => null, 2 => null, 3 => null, 4 => null,5 => null, 6 => null);


if (array_key_exists($output['responsecode'], $bads))
    ....
Artefacto
@Artefacto, the values returned are alpha characters (A,B,C)
JM4
It doesn't matter. Array keys can be strings.
Artefacto
I am receiving the following error: Warning: in_array() expects parameter 2 to be array, null given in...
JM4
see edit - getting errors
JM4
+6  A: 

in_array

if(in_array($output['responsecode'], $bads))
{

}

Also, if your codes are sequential, you can use range to generate the array.

$bad = range(1, 10);
Tim Cooper
see edit - errors with code
JM4
A: 

How about you have a $errors array and only add to it if there's an error. If the $errors array is not empty, echo "All aboard the fail train!"

glowcoder
Then you still need something to check if the response is good or bad.
Alec