PHP in_array没有返回预期的结果

When I attempt to determine if a user is in an array of users, for some reason it is only returning true when the user is in the 0th position.

For the life of me I cannot figure out what I am doing wrong.


This does not echo "True"

echo $usersign; // RDW

print_r($these_analysts[0]); // Array ( [0] => JKB [1] => RDW )

if(in_array($usersign,$these_analysts[0])){
    echo "True";
}

This echoes "True"

echo $usersign; // RDW

print_r($these_analysts[0]); // Array ( [0] => RDW [1] => CLM )

if(in_array($usersign,$these_analysts[1])){
    echo "True";
}

EDIT:

vardump gives a much more comprehensive view of the array, whereas print_r did show the trailing spaces, it didn't catch my eye.

For some reason the first element of each array was giving string3, and all others were giving string4.

You're missing ; on half of your lines, you're using base strings instead of " around them, and your Array syntax is invalid (should be Array("JKB","RDW");). Maybe if these are fixed it might have a chance of working.

You have punctuation errors:

$these_analysts[0] = Array ( [0] => JKB [1] => RDW )

should be

$these_analysts = Array ( 0 => "JKB", 1 => "RDW" );

You have a lot of syntax errors.

When you use strings, it is always better prace to put strings in single or double quotes. It doesn't matter which one (as far as speed is concerned).

Also, you need commas between the elements.

I entered the following code and it works.

$usersign = 'RDW';  
$these_analysts[0] = array( 'JKB', 'RDW' );    
print_r( $these_analysts );   
if(in_array($usersign,$these_analysts[0])) echo "True"; 

Try:

$usersign = 'RDW';

$these_analysts[1] = Array ( 0 => 'RDW', 1 => 'CLM' );

if(in_array($usersign,$these_analysts[1])){
       echo "True";
}

That should work.

This is happening (at least in my testing) if you specify RDW as a constant without defining these constants before using them. If you put your initials in double-quotes (i.e. use explicit strings) then everything works fine. If you want to use them as constants, then define these constants first:

define("RDW","RDW");
define("JKB","JKB");

And then your code works as expected again.

*This is the actual way you need to do *

$usersign = 'RDW';
$these_analysts = array ( 0 => 'RDW', 1 => 'CLM' );
if(in_array($usersign,$these_analysts)){
   echo "True";
}