2013-12-18 32 views
1

我有這樣的代碼在這裏:PHP如果不是在陣列

if (in_array("Notecards", $array) || in_array("Poster", $array)){ 
    echo "Match found"; 
}else{ 
    echo "Match not found"; 
} 

我的問題是,海報可能是海報1海報2海報3海報4等

如果客場說海報然後空間然後一個數字不在我的數組?

+0

你能後的樣本output.Its沒有太多明確你問 –

+0

是'$ array'排序? –

+0

$ array沒有排序,但可能有像Poster 1或Poster 3或Poster 2這樣的值......我只是想找出是否「海報」,然後「」,然後是我的數組中的任何數字。 – user1269625

回答

8

使用preg_grep像這樣:

if (in_array("Notecards", $array) || preg_grep("/Poster\s\d+/", $array)){ 
    echo "Match found"; 
}else{ 
    echo "Match not found"; 
} 

我把假設你可能比海報更0-9的自由,在這種情況下\d+匹配一個或多個數字(海報10,海報800等)。如果您只需要一位數字,請刪除+

0

這是一個解決辦法:

if (in_array("Notecards", $array) || substr_in_array("Poster", $array)){ 
    echo "Match found"; 
}else{ 
    echo "Match not found"; 
} 

substr_in_array($str, $array){ 
    $length = strlen($str); 
    foreach($array as $value){ 
     if(substr($value, 0, $length) == $str){ 
      return true; 
     } 
    } 
    return false; 
}