I have a MySQL table with a tree data structure. The fields are _id
, name
and parentId
. When the record hasn\'t a parent, parent
Your problem is already solved within the SPL. The RecursiveIteratorIteratorDocs has the information about one's item's depth:
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array), SELF_FIRST);
foreach ($it as $key => $element)
{
if ($key !== 'name') continue;
$inset = str_repeat('--', $it->getDepth());
printf('', $inset, $element);
}