0

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??

4
  • 1
    Read about $exists operator Commented Aug 10, 2012 at 11:34
  • Do you want to check only if array exists, or if it exists and holds certain elements? Commented Aug 10, 2012 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. Commented Aug 10, 2012 at 11:37
  • An "undefined index" warning is telling you that just that one array element does not exist, not necessarily the entire array. Commented Aug 10, 2012 at 11:40

2 Answers 2

2

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

array('array_name' => array('$exists' => true))
Sign up to request clarification or add additional context in comments.

Comments

1

you can check the index existence like this:

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

    }

OR

array_key_exists()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.