I created this array with a circular reference:
$arr = array(1 => 'one', 2 => 'two');
$arr[3] = &$arr;
I have a function that recursively outputs values โโin an array, but I really could not solve the problem of creating a circular check. How can you do this?
The current function that I have to print the array is copied below. I did not include the various attempts I made during the round check. They basically revolved around a strategy to support an array of $seenelements that were already printed for each recursion branch. This is because I still want to allow the printing of duplicate values, and not print the value if it is the parent of the current array that is being processed.
I am having problems with how to add links, not copies of arrays to this variable $seen. But I would be happy to use a different strategy together if it worked.
function HTMLStringify($arr)
{
if(is_array($arr)){
$html = '<ul>';
foreach ($arr as $key => $value) {
$html .= '<li>' . $key;
if(is_array($value)){
$html .= HTMLStringify($value, $seen);
}
elseif(is_numeric($value) || is_string($value) || is_null($value))
{
$html .= ' = ' . $value;
}
else
{
$html .= ' [couldn\'t parse ' . gettype($value) . ']';
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
else
{
return null;
}
}
source
share