0

如何检查数组是否存在于php中。

我在 mongodb 集合中的某些行中有一个名为 $contact["categories"] 的数组。有些行没有那个数组。如何检查集合中是否存在特定数组?

4

2 回答 2

2

使用 $exists 参数检查元素是否存在。

array('array_name' => array('$exists' => true))
于 2012-08-10T11:49:55.473 回答
1

您可以像这样检查索引是否存在:

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

    }

或者

array_key_exists()

if( array_key_exists('categories', $contact) ) {
}
于 2012-08-10T11:39:17.360 回答