2014-06-13 58 views
-1

如何從$sku變量中刪除一些值?它從DB返回2110,1630,4565等。從php變量中刪除值

代碼:

$products = SProductVariantsQuery::create()->find()->toArray(); 

foreach ($products as $variant) { 
$sku = $variant['Number']; 
} 

的var_dump($ SKU)返回此:

string(4) "2250" string(4) "2251" string(3) "428" string(3) "427" string(4) "2800" string(4) "2804" 
+0

你可以添加一個'var_dump()'你的變量,所以我們可以看到它的結構? – Machavity

+0

另外,請給你想要的輸出... –

+1

請看看http://stackoverflow.com/questions/1883421/removing-array-item-by-value –

回答

1

創建值,你要你做的foreach,然後檢查值之前排除數組有:

// Exclude prod. no 427 and 2800 
$exclude = array('427', '2800'); 

foreach ($products as $variant) { 
    // Only set $sku if the Number is not in the exclude array 
    if (!in_array($variant['Number'], $exclude)) { 
     $sku = $variant['Number']; 
    } 
}