Check arrays for recursion

前端 未结 5 1071
误落风尘
误落风尘 2021-02-05 22:29

What is the best way to check if an array is recursive in PHP ?

Given the following code:



        
5条回答
  •  广开言路
    2021-02-05 23:14

    I believe you can't check that. Read the ReferenceDoc for more information on reference.

    Here is the function to check for RECURSION (from PHP doc comments), altough it seems to be very slow (I would not suggest it):

      function is_array_reference ($arr, $key) {
            $isRef = false;
            ob_start();
            var_dump($arr);
            if (strpos(preg_replace("/[ \n\r]*/i", "", preg_replace("/( ){4,}.*(\n\r)*/i", "", ob_get_contents())), "[" . $key . "]=>&") !== false)
                $isRef = true;
            ob_end_clean();
            return $isRef;
        }
    

提交回复
热议问题