1

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.

4
  • 2
    Please add result of var_dump to your question for $these_analysts
    – Vyktor
    Feb 3, 2012 at 18:22
  • 2
    Did you mean to use the string "RDW" or is RDW a constant?
    – gen_Eric
    Feb 3, 2012 at 18:23
  • 2
    And add error_reporting(-1) to your php to show all possible errors/warnings/notices/whatevers.
    – biziclop
    Feb 3, 2012 at 18:24
  • I must clarify, the first two lines were just meant to show the value the variable and arrays had taken. I wasn't actually setting them there. Sorry about that confusion. Feb 3, 2012 at 21:37

6 Answers 6

4

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"; 
2
  • I wouldn't put the numeric array keys into quotes though... They are numbers.
    – Svish
    Feb 3, 2012 at 18:38
  • i wouldn't put them at all. i took them out. i agree though the integers should generally stay out of quotes, but that php doesn't matter. i try to be consistent on that one but forget still.
    – phpmeh
    Feb 3, 2012 at 18:40
2

Try:

$usersign = 'RDW';

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

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

That should work.

1
  • Yeah, I noticed that right after I posted it. You were too quick, it's fixed now :)
    – Jim D
    Feb 3, 2012 at 18:29
1

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.

3
  • Even though you can call constants as echo RDW; I've heard (and it makes sense to me) that better practice is to still use echo constant("RDW");
    – phpmeh
    Feb 3, 2012 at 18:31
  • The issue is that he's not defining the constants before using them - hence the constant is explicitly defined each time it's seen - and as the second definition overrides the first one, the two are not the same constant, therefore in_array doesn't work.
    – Aleks G
    Feb 3, 2012 at 18:32
  • 1
    I'm not sure constants were applicable here (vs OP just making a mistake) but +1 for your strong command of constants usage!
    – phpmeh
    Feb 3, 2012 at 18:36
0

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.

0

You have punctuation errors:

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

should be

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

*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";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.