简体   繁体   English

PHP in_array未返回预期结果

[英]PHP in_array not returning expected results

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. 当我尝试确定用户是否在用户数组中时,由于某种原因,它仅在用户位于第0位时才返回true。

For the life of me I cannot figure out what I am doing wrong. 对于我的一生,我无法弄清楚自己在做错什么。


This does not echo "True" 这不会回显“ 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. vardump给出了数组的更全面的视图,而print_r确实显示了尾随空格,但并没有引起我的注意。

For some reason the first element of each array was giving string3, and all others were giving string4. 由于某些原因,每个数组的第一个元素都给定string3,所有其他元素都给定string4。

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. 如果您将RDW指定为常量而不在使用前定义这些常量,就会发生这种情况(至少在我的测试中)。 If you put your initials in double-quotes (ie 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. 然后,您的代码将再次按预期工作。

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. 在一行的一半上,您使用的是基本字符串而不是“, " ,并且您的Array语法无效(应为Array("JKB","RDW"); )。也许,如果这些字符串已修复,则可能会有机会工作。

You have punctuation errors: 您有标点错误:

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

should be 应该

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM