2012-08-10 102 views
0

如何檢查數組是否存在於php中。檢查數組是否存在於php mongodb中?

我在mongodb集合中的某些行中調用$ contact [「categories」]數組。 某些行沒有該數組。如何檢查集合中是否存在特定數組?

+1

閱讀關於'$ exists'運算符 – 2012-08-10 11:34:23

+0

您是否想檢查數組是否存在,或者它是否存在並持有某些元素? – 2012-08-10 11:35:21

+0

只檢查數組是否存在? $存在數組上工作嗎?當我嘗試使用$ contact [「categories」]時,因爲它不存在,所以將錯誤作爲未定義的索引引發。 – user1578749 2012-08-10 11:37:45

回答

1

您可以檢查這樣的指標是否存在:

if (isset($contact["categories"])) { 

    } 

OR

array_key_exists()

if(array_key_exists('categories', $contact)) { 
} 
2

使用$存在參數來檢查元素是否存在。

array('array_name' => array('$exists' => true))