Nesting level too deep - recursive dependency?

Description

PHP comparison may be recursive: it happens when an array contains a reference to another element of itself. In that case, the array becomes recursive: traversing such array may end up with an infinite loop, as the reference may loop back on itself.

This error message applies to array comparisons, and count(). There might be other situations where this applies.

Example

<?php

$a = [1, 5=> &$a];
$b = [1, 5=> &$b];

try {
    var_dump($a === $b);
} catch (Error $e) {
    print caught\n;
}

// count() doesn't handle recursive arrays
count($a);

?>

Solutions

  • Do not make direct comparisons of recursive arrays.

  • Do not try to count recursive arrays.