2013-02-08 81 views
-1

我有這樣一個var_dump($productId)排序一些ID與某些數字的PHP字符串中

string(1) "9" 

string(2) "11" 

string(2) "12" 

string(2) "17" 

string(2) "18" 

現在,我想例如function_x不爲ID 18和17執行的,所以我要做出這樣的事情:

$test=array('18','17'); 
if(!in_array($test,array($productId))){} 

,但似乎並不奏效,這個想法是,如果我想爲剛纔18則:

$test=18; 
if($test != $productId){} 

,這是工作,但如何做到多個號碼/ ID?

在此先感謝!

+1

從你的var_dump($ productId)看起來它不是一個數組。無論如何。如果它不是安東尼的答案會做的。 – Redian

回答

1

我覺得其他的答案是假設你$productId是一個數組。我猜你正在運行var_dump()內部的foreach或其他東西。

if(17 != $productId && 18 != $productId) 
{ 
    // If $productId is not 17 and is not 18. 
} 

// Alternatively, if you want to add more IDs to ignore, you could use in_array. 
$ignore = array(17,18,19,20); 

if(false == in_array($productId,$ignore)) 
{ 
    // If $productId is not in the ignore array. 
} 
+0

是的,var_dump是在另一個愚蠢的magento foreach,它似乎是正確的如何你的代碼是,但我的問題是,我有很多數字不只是兩個,我需要一個解決方案的很多數字 – Abude

+0

抱歉沒有看到另外,我會給它一個嘗試:) – Abude

+0

這正是我需要的,非常感謝你'湯姆',它的工作! – Abude

0

如果你得到$productId作爲一個字符串一次一個:

if(!in_array($productId,$test)) { 
    // execute function 
} 

如果$productId是一個數組:

$test=array('18','17'); 
foreach ($productId as $id) { 
    if(!in_array($id,$test)) { 
     // execute function 
    } 
} 
+0

我會試試看看它是否有效 – Abude

+0

它刪除了所有的id不僅18,17 ... – Abude

+0

@Jimmy看到更新的答案。我認爲'$ productId'是一個數組。 – Antony