【问题标题】:create a unlimited multilevel nested list from a recursive array从递归数组创建无限多级嵌套列表
【发布时间】:2018-09-06 19:33:21
【问题描述】:

任何人都可以帮助我循环遍历递归数组并使用 php 创建多级嵌套列表的逻辑。我在超越顶级列表节点方面并不是很成功。

请看下面的数组:

Dump => array(6) {
  [0] => array(5) {
    ["id"] => string(1) "1"
    ["label"] => string(13) "address types"
    ["slug"] => string(17) "admin/addresstype"
    ["parent_id"] => string(1) "0"
    ["has_children"] => bool(false)
  }
  [1] => array(5) {
    ["id"] => string(1) "3"
    ["label"] => string(9) "dashboard"
    ["slug"] => string(15) "admin/dashboard"
    ["parent_id"] => string(1) "0"
    ["has_children"] => bool(false)
  }
  [2] => array(6) {
    ["id"] => string(1) "5"
    ["label"] => string(16) "feature category"
    ["slug"] => string(21) "admin/featurecategory"
    ["parent_id"] => string(1) "0"
    ["has_children"] => bool(true)
    ["children"] => array(2) {
      [0] => array(6) {
        ["id"] => string(1) "4"
        ["label"] => string(7) "feature"
        ["slug"] => string(13) "admin/feature"
        ["parent_id"] => string(1) "5"
        ["has_children"] => bool(true)
        ["children"] => array(2) {
          [0] => array(5) {
            ["id"] => string(1) "2"
            ["label"] => string(6) "status"
            ["slug"] => string(12) "admin/status"
            ["parent_id"] => string(1) "4"
            ["has_children"] => bool(false)
          }
          [1] => array(5) {
            ["id"] => string(1) "7"
            ["label"] => string(4) "menu"
            ["slug"] => string(10) "admin/menu"
            ["parent_id"] => string(1) "4"
            ["has_children"] => bool(false)
          }
        }
      }
      [1] => array(5) {
        ["id"] => string(1) "9"
        ["label"] => string(17) "subscription type"
        ["slug"] => string(22) "admin/subscriptiontype"
        ["parent_id"] => string(1) "5"
        ["has_children"] => bool(false)
      }
    }
  }
  [3] => array(5) {
    ["id"] => string(1) "6"
    ["label"] => string(8) "industry"
    ["slug"] => string(14) "admin/industry"
    ["parent_id"] => string(1) "0"
    ["has_children"] => bool(false)
  }
  [4] => array(5) {
    ["id"] => string(1) "8"
    ["label"] => string(12) "subscription"
    ["slug"] => string(18) "admin/subscription"
    ["parent_id"] => string(1) "0"
    ["has_children"] => bool(false)
  }
  [5] => array(5) {
    ["id"] => string(2) "10"
    ["label"] => string(4) "user"
    ["slug"] => string(10) "admin/user"
    ["parent_id"] => string(1) "0"
    ["has_children"] => bool(false)
  }
}

在此先感谢

【问题讨论】:

  • “嵌套列表”是指HTML 还是ulol
  • 是的 HTML
      ,干杯。
  • 如果您发布您的尝试会更好,然后人们可以更正/添加它,而不必自己做所有事情。这看起来你根本没有付出太多努力。
  • 您可以查看此评论以了解非嵌套数组。 stackoverflow.com/questions/23916237/…

标签: php associative-array


【解决方案1】:

你可以使用这样的东西

/*
* Builds a tree based on parent child relationships
* @data: relationship data
* @parent: level to start at
*/
function buildTree(Array $data, $parent = 0) {
    $tree = array();
    foreach ($data as $d) {
        if ($d['ParentID'] == $parent) {
            $children = buildTree($data, $d['id']);
            // set a trivial key
            if (!empty($children)) {
                $d['_children'] = $children;
            }
            $tree[] = $d;
        }
    }
    return $tree;
}

function printTree(Array $data, $markup = ''){
    foreach($data as $elm){
        echo '<li data-id="'.$elm['TrainingID'].'">'.$elm['trainingName'].'<a href="#pages|admin|training?parent='.$elm['TrainingID'].'"><span class="addChild" title="Add a child training"></span></a><span class="editTraining" title="Edit"></span>';
        if(isset($elm['_children']))
        {
            echo '<ul>';
            printTree($elm['_children'], $markup);
            echo '</ul>';
        }
        print '</li>';
    }
}

【讨论】:

  • 这是完美的作品,就像一个魅力,我知道我的递归哪里出错了。一百万谢谢你;-)
  • 很好的答案,只是一个问题,尽管 $markup 的意义是什么?
  • 可能是我使用该变量来存储生成的 html,忽略它的存在甚至删除它。
  • printTree() 产生无效的 HTML,在父 &lt;ul&gt; 内嵌套了一个 &lt;ul&gt;。子 &lt;ul&gt; 应该在父 &lt;li&gt; 内。我会建议修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
相关资源
最近更新 更多