简体   繁体   English

in_array在Laravel 5中无法正常工作

[英]in_array not working properly in Laravel 5

in_array function is returning 1 if values exists, otherwise it returns an empty string. 如果值存在, in_array函数将返回1,否则将返回一个空字符串。 I am expecting it to return true or false . 我期望它返回truefalse Can someone please help me with this? 有人可以帮我吗?

In PHP the integer value 1 is converted to true , check out the documentation on booleans in PHP here . 在PHP中,整数值1转换为true ,请在此处查看有关布尔值的文档。

You could use it as a boolean like so 您可以像这样将其用作布尔值

$arrBoolean = in_array("needle", $arr);
if ($arrBoolean) {
    // Code
}

If you want it to say true or false you could do this 如果您希望它说的是true还是false ,可以这样做

$arrBoolean = in_array("needle", $arr) ? 'True' : 'False';
echo($arrBoolean);

However, if you do that, then they are String representations of a boolean and do not actually work as a boolean, they will be just Strings. 但是,如果执行此操作,则它们是布尔值的String表示形式,实际上并不用作布尔值,它们将只是Strings。

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

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