我试图用数组检查PHP中的许多值

时间:2016-06-24 00:51:59

标签: php

数组条件有什么问题?即使corps等于这些值中的任何一个,它也不会进入if条件

while($row = $req->fetch()){

  $corps= $row->NVCDC;
  $list_ex = array("201","20","25","204");
  if(in_array($corps, $list_ex)){
    $daten = $row->DTN;
    $daterec = $row->DTR;
    if($daten < $daterec ){
      //do something
    }

1 个答案:

答案 0 :(得分:0)

<?php
//If $corps is a array u must do it this way
$corps = ["201","20"];
$list_ex = array("201","20","25","204");
foreach($corps as $corp)
{
    if(in_array($corp, $list_ex))
        echo "yes\n";
} //Will echo Yes 2 time

//If $corps is a string or number
$corps = "201";
$list_ex = array("201","20","25","204");
if(in_array($corps, $list_ex))
{
    echo "yes";
}//Will echo yes 1 time
?>

仔细检查$ corps的值,它可能是一个数组,或者值不是你想象的那样,如果它是一个数组,那么同样的“数组”必须存在于$ list_ex < / p>

如果$ corps == [20,201] $ list必须有$ list [[20,201]] for_array才能返回TRUE