我可以在in_array()中使用substr()作为指针吗?(Can I use substr() as the needle in in_array()?) $haystack = 'I am a haystack. Hear me rawr.'; $pos = strlen($haystack); $nlen = 1; $needle = array('.', '. '); print_r(in_array(substr($haystack, $pos, $nlen), $needle, true));

我无法弄清楚为什么会失败。 我试图看一下针的数组是否匹配了从大海捞针中选择的结果? 如何将该值作为布尔值返回?

$haystack = 'I am a haystack. Hear me rawr.'; $pos = strlen($haystack); $nlen = 1; $needle = array('.', '. '); print_r(in_array(substr($haystack, $pos, $nlen), $needle, true));

I am having trouble figuring out why this is failing. I am trying to see if an array of needles matches the result that substr chooses from the haystack? How can I return that value as boolean?

最满意答案

是和否,因为substr返回一个字符串 ,在你的情况下是针,在失败时返回FALSE ,在这种情况下它不是in_array函数的有效参数。

您应该首先使用substr提取字符串的一部分,并且需要确保提取了一些字符串并且它没有返回FALSE ,然后才应该在in_array使用它。

Yes & NO because substr returns a string which is needle in your case and FALSE on failure in which case it won't be a valid argument to in_array function.

You should first extract a part of string using substr and need to make sure that you extracted some string and it did not return FALSE, only then you should use it in in_array.

更多推荐