title: Is there a way to detect circular arrays in pure PHP?
body: <p>I'm trying to implement my own serialization / var_dump style function in PHP. It seems impossible if there is the possibility of circular arrays (which there is).</p>
  
  <p>In recent PHP versions, var_dump seems to detect circular arrays:</p>
  
  <pre><code>php &gt; $a = array();
  php &gt; $a[] = &amp;$a;
  php &gt; var_dump($a);
  array(1) {
    [0]=&gt;
    &amp;array(1) {
      [0]=&gt;
      *RECURSION*
    }
  }
  </code></pre>
  
  <p>How would I implement my own serialization type of method in PHP that can detect similarly? I can't just keep track of which arrays I've visited, because strict comparison of arrays in PHP returns true for different arrays that contain the same elements and comparing circular arrays causes a Fatal Error, anyways.</p>
  
  <pre><code>php &gt; $b = array(1,2);
  php &gt; $c = array(1,2);
  php &gt; var_dump($b === $c);
  bool(true)
  php &gt; $a = array();
  php &gt; $a[] = &amp;$a;
  php &gt; var_dump($a === $a);
  PHP Fatal error:  Nesting level too deep - recursive dependency? in php shell code on line 1
  </code></pre>
  
  <p>I've looked for a way to find a unique id (pointer) for an array, but I can't find one. spl_object_hash only works on objects, not arrays. If I cast multiple <em>different</em> arrays to objects they all get the same spl_object_hash value (why?).</p>
  
  <p>EDIT:</p>
  
  <p>Calling print_r, var_dump, or serialize on each array and then using some mechanism to detect the presence of recursion as detected by those methods is an algorithmic complexity nightmare and will basically render any use too slow to be practical on large nested arrays.</p>
  
  <p>ACCEPTED ANSWER:</p>
  
  <p>I accepted the answer below that was the first to suggest temporarily altering the an array to see if it is indeed the same as another array. That answers the "how do I compare two arrays for identity?" from which recursion detection is trivial.</p>