Last Updated: February 25, 2016
· malteo

If you get a "Nesting level too deep" error while using in_array on a Doctrine Collection

When Doctrine returns a bunch of objects they are encapsulated in an ArrayCollection, which allows you to treat them as a simple PHP array, among others.

This means you can, for example, run a

in_array($object, $doctrineCollection);

and expect to find the proverbial needle in the haystack.

But if you are dealing with objects of medium complexity, you may step into this error

“Nesting level too deep – recursive dependency?”

As explained here

It would appear that by default in_array does non-strict comparison (equivalent to an == operation) when testing the haystack for the needle. This means that it checks that all the properties are equal, which means it starts traversing the object graph, and that can get you into trouble if you have circular references in that graph.


The in_array function has a strict mode, however, which as far as I can tell does the equivalent of an === operation.

which means that you should instead check if

in_array($object, $doctrineCollection, TRUE);
Say Thanks