【问题标题】:php display multilevel treenode menu recursivephp显示多级treenode菜单递归
【发布时间】:2014-04-01 02:38:11
【问题描述】:

我有一个关于这个的帖子,虽然我现在有一个与同一主题相关的不同问题。您可能想在此处参考原始问题。 php display multilevel treenode menu

现在数组是按树节点显示的,它似乎不是完全递归的。我注意到当第一个元素是子节点时,它不会显示为子节点。

我尝试先进行 krsort 然后再进行 ksort。

function getChildren(&$rows, $p = 0) {
    $r = array();
    krsort($rows);
    foreach($rows as $row_id => $row) {
        if ($row['parent_node']==$p) {
            $r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
            unset($rows[$row_id]);
        }
    }
    ksort($rows);
    return $r;
}

数组的结构如下:

array
  0 => 
    array
      'product_category_code' => string 'akamia' (length=6)
      'product_category_desc' => string 'Akamia' (length=6)
      'parent_node' => string 'summer-dress' (length=12)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-04-01 10:03:42' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-04-01 10:03:47' (length=19)
  1 => 
    array
      'product_category_code' => string 'bracelets' (length=9)
      'product_category_desc' => string 'Bracelets' (length=9)
      'parent_node' => string '' (length=0)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:04:08' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-03-14 22:09:05' (length=19)
  2 => 
    array
      'product_category_code' => string 'floral-dress' (length=12)
      'product_category_desc' => string 'Floral Dress' (length=12)
      'parent_node' => string '' (length=0)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:09:49' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-04-01 10:03:30' (length=19)
  3 => 
    array
      'product_category_code' => string 'flowery-bracelets' (length=17)
      'product_category_desc' => string 'Flowery Bracelets' (length=17)
      'parent_node' => string 'bracelets' (length=9)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:09:16' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-03-30 19:08:44' (length=19)
  4 => 
    array
      'product_category_code' => string 'small-flowery-bracelets' (length=23)
      'product_category_desc' => string 'Small Flowery Bracelets' (length=23)
      'parent_node' => string 'flowery-bracelets' (length=17)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:08:35' (length=19)
      'modified_by' => string '1' (length=1)
      'modified_date' => string '2014-03-30 19:09:44' (length=19)
  5 => 
    array
      'product_category_code' => string 'summer-dress' (length=12)
      'product_category_desc' => string 'Summer Dress' (length=12)
      'parent_node' => string '' (length=0)
      'inactive' => string '0' (length=1)
      'sort' => string '0' (length=1)
      'created_by' => string '1' (length=1)
      'created_date' => string '2014-03-14 22:09:29' (length=19)
      'modified_by' => string '0' (length=1)
      'modified_date' => null

输出(没有 krsort / ksort)显示这个,这是错误的:

  • Akamai
  • 手链
    • 花朵手链
      • 小花手链
  • 碎花连衣裙
  • 夏季连衣裙

这应该是输出:

  • 手链
    • 花朵手链
      • 小花手链
  • 碎花连衣裙
  • 夏季连衣裙
    • Akamai

【问题讨论】:

    标签: php recursion drop-down-menu treenode multi-level


    【解决方案1】:

    没关系,我得到了答案。 function getChildren(&$rows, $p = 0) 上的 $p 应该是 $p = '' 而不是零 (0),因为数组上的父节点为空。傻我。呵呵

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 2013-11-05
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多