Take the 2-minute tour ×

how to check if the array exists in php.

I ve the array called $contact["categories"] in some of the rows in mongodb collection. some rows doesn't have that array. how to check if particular array exists in collection??

share|improve this question
Read about $exists operator –  Sergio Tulentsev Aug 10 '12 at 11:34
Do you want to check only if array exists, or if it exists and holds certain elements? –  Sergio Tulentsev Aug 10 '12 at 11:35
just check only if the array exists?? does $exist work on arrays as well?? when i try to use $contact["categories"], since it doesnt exits it is throwing error as undefined index. –  user1578749 Aug 10 '12 at 11:37
An "undefined index" warning is telling you that just that one array element does not exist, not necessarily the entire array. –  w3d Aug 10 '12 at 11:40
add comment

2 Answers

up vote 1 down vote accepted

you can check the index existence like this:

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




if( array_key_exists('categories', $contact) ) {
share|improve this answer
add comment

Use $exists parameter to check whether element exists or not.

array('array_name' => array('$exists' => true))
share|improve this answer
add comment

Your Answer


