Test if variable contains circular references

假装没事ソ 提交于 2019-12-05 05:54:08
BIOS

Hacky but returns true based on the circular example you gave:

<?php
// create the circular reference
$r = array();
$r[] = &$r;

function isRecursive($array){
  $dump = print_r($array, true);
  if(strpos($dump, '*RECURSION*') !== false)
      return true;
  else
      return false;
}

echo isRecursive($r); // returns 1

Interested to see what else people come up with :)

Would this do it?

function isRecursive($array) {
    foreach($array as $v) {
        if($v === $array) {
            return true;
        }
    }
    return false;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!