PHP有一个系统函数is_array()可以判断一个值是否在数组中。
语法如下:
in_array(value,array,type) return boolen
示例一:普通使用
代码:
$str = 1; $arr = array(1,3,5,7,9); $boolvalue = in_array($str,$arr); var_dump($boolvalue);
bool(true)
$str = '1'; $arr = array(1,3,5,7,9); $boolvalue = in_array($str,$arr,false); var_dump($boolvalue);
bool(true)
$str = '1'; $arr = array(1,3,5,7,9); $boolvalue = in_array($str,$arr,true); var_dump($boolvalue);
bool(false)
class a { public $a = 1; public function fun(){ return $this->a; } } class b { public $a = 2; public function fun(){ return $this->a; } } $a = new a(); $b = new b(); $c = clone $a; $arr = array($a,$b); $boolvalue = in_array($c,$arr,false); var_dump($boolvalue);
bool(true)
class a { public $a = 1; public function fun(){ return $this->a; } } class b { public $a = 2; public function fun(){ return $this->a; } } $a = new a(); $b = new b(); $c = clone $a; $arr = array($a,$b); $boolvalue = in_array($c,$arr,true); var_dump($boolvalue);
bool(false)
$str = 10; $arr = array( array(1,2,3,4), array(5,6,7,8,9), 10 ); $boolvalue = in_array($str,$arr); var_dump($boolvalue);
bool(true)
$str = 10; $arr = array( array(1,2,3,4), array(5,6,7,8,9,10), ); $boolvalue = in_array($str,$arr); var_dump($boolvalue);
bool(false)