简体   繁体   English

为什么 in_array 为这个搜索返回 false?

[英]Why does in_array return false for this search?

Why does below code return false ?为什么下面的代码返回false It ought to return true.它应该返回true。

in_array(
    '/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg',
    [
        'https://example.com/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg'
    ],
    false
)

Online code editor在线代码编辑器

It is correct behavior.这是正确的行为。

There is no string /wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg in that array.该数组中没有字符串/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg

I think you have misunderstood the "strict" argument.我认为您误解了“严格”​​的说法。 Eg.例如。

in_array('3', [1, 2, 3, 4], true); // ==> false because the string '3' !== 3

However if you don't pass a third argument or pass it false :但是,如果您不传递第三个参数或传递false

in_array('3', [1, 2, 3, 4]);        // ==> true because '3' == 3
in_array('3', [1, 2, 3, 4], false); // ==> true because '3' == 3

However in your case the strings don't match even with loose == :但是,在您的情况下,即使使用松散的== ,字符串也不匹配:

'/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg' == 
    'https://example.com/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg'
// ==> false

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

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