Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use memory<s>, Luke</s>. When your iterator comes across the array as an elementof another one, just store reference/id of it to smth like set or list (or another suitable container, eg. array). So you memorize what array you have already processed, and then ignore it when meet next time or stop the loop.</p> <pre><code>&lt;?php function get_id(&amp;$array) { return crc32(serialize($array)); } # This will not work as expected. I leave it here just for future versions. function get_id_alt(&amp;$array) { $obj = (object) $array; return spl_object_hash($obj); } function iterate(array $input, &amp;$reached=array(), $id_func='get_id') { // this is an unseen array, memorize it array_push($reached, $id_func($input)); foreach($input as $key=&gt;$item) { if (is_array($item)) { // This is an array if (in_array($id_func($item), $reached)) { // We found an array that have been processed before. What will you do? } else { // Recurse! iterate($item, $reached); } } else { // It is not traversable value, do what you want here } } } </code></pre> <p>PS: I'm using <a href="http://php.net/manual/en/function.spl-object-hash.php" rel="nofollow">spl_object_hash</a> as a ID-function, you may use another one, if prefered (but i don't know others that can identify the same objects, as this one does).</p> <p><strong>UPD:</strong> Using <code>spl_object_hash</code> doesn't give right results as of PHP 5.3.10: it treats any array as the same object regardless of its content. But using smth like <code>%hash_fn%(serialize($array))</code> works well (beware of performance degradation!).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload