How to recursively build a <select> with unknown tree depth

后端 未结 8 607
栀梦
栀梦 2020-12-15 01:49

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

8条回答
  •  一生所求
    2020-12-15 02:07

    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);
    }
    

提交回复
热议问题