本文介绍了in_array()不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此数组($options):

Array (
    [0] => 0
    [1] => 1
    [2] => 2
)

PHP返回TRUE:

PHP returns TRUE:

$this->assertTrue( in_array('Bug', $options ) );         // TRUE
$this->assertTrue( in_array('Feature', $options ) );     // TRUE
$this->assertTrue( in_array('Task', $options ) );        // TRUE
$this->assertTrue( in_array('RockAndRoll', $options ) ); // TRUE

为什么?

推荐答案

这是因为 0 == "string"是正确的,而0是数组的元素.

This is because 0 == "string" is true, and 0 is an element of the array.

in_array中的参数$strict设置为true:

Set the parameter $strict in in_array to true:

$this->assertTrue( in_array('Bug', $options, true) );

这篇关于in_array()不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:16
查看更多