2011-05-23 24 views
2

我有一個方法,fetch_widgets,從(MySQL)數據庫中獲取小部件。我將它傳遞一個$options陣列所以可以有選擇地添加WHEREJOIN子句,添加/刪除列等PHP數組:索引與鍵控

例如:

$options = array(); 
    $options[] = 'include_disabled'; 
    $options[] = 'include_tag_ids'; 
    $options['start_date'] = '2011-01-01'; 
    $options['end_date'] = '2011-01-31'; 

fetch_widgets我使用檢查的選項之一:

if(array_key_exists('start_date',$options)) { ... } 

或:

if(in_array('include_tag_ids',$options)) { ... } 

取決於該選項是否被激活而僅存在(例如, include_disabled)或具有密鑰和值(例如end_date)。

我遇到了困難,因爲in_array$options數組包含鍵值和非鍵值時,我得到了奇怪的結果。任何人都可以對此有所瞭解嗎?

+2

什麼是你所得到的奇怪的行爲? – 2011-05-23 19:53:33

+0

鑑於上述'$ options'數組我有'in_array('include_tag_ids',$ options)'return false。我似乎已經解決了它,不知道我改變了什麼,但它再次工作 – Matthew 2011-05-23 21:21:03

回答

5

如果您需要一致的行爲,請勿混合/匹配鍵控和非鍵控陣列。

相反,做這樣的事情:

$options = array(); 
$options['include_disabled'] = true; 
$options['include_tag_ids'] = true; 
$options['start_date'] = '2011-01-01'; 
$options['end_date'] = '2011-01-31'; 
+1

+1我絕對同意,但我仍然有興趣看到他遇到的問題,爲什麼。 – Wiseguy 2011-05-23 20:45:10

+0

我也很好奇。根據他展示的內容,它應該在理論上起作用,但是誰知道。 – gnxtech3 2011-05-23 20:47:22

+0

我很好奇!如果它再次出現,我一定會回來。 – Matthew 2011-05-23 21:22:09