列出具有类别>子类别逻辑的多维数组

时间:2014-12-27 21:41:14

标签: php arrays multidimensional-array

我有一个存储根类别的所有子类别的数组,例如:

我只想这样列出:

  • A
  • A>乙
  • A> B&以及c
  • A&GT B个c取代,E
  • A> B个d
  • A&GT B个d取代;˚F
  • A&GT B个克
  • A&GT B个g取代; H

我的阵列:

Array (
  [0] => Array (
    [id] => 2
    [title] => B
    [sub_category] => Array (
      [0] => Array (
        [id] => 3
        [title] => C
        [sub_category] => Array (
          [0] => Array (
            [id] => 5
            [title] => E
            [sub_category] => Array ()
          )
        )
      )
      [1] => Array (
        [id] => 4
        [title] => D
        [sub_category] => Array (
          [0] => Array (
            [id] => 6
            [title] => F
            [sub_category] => Array ()
          )
        )
      )
      [2] => Array (
        [id] => 7
        [title] => G
        [sub_category] => Array (
          [0] => Array (
            [id] => 10
            [title] => H
            [sub_category] => Array ()
          )
        )
      )
    )
  )
)

我的功能是列出没有'>'且没有父母,只有标题。

以下是我的功能:

function buildTree($tree) {
    foreach ($tree as $node)
    {
        echo '<li>'.$node['title'].'</li>';
        if (!empty($node['sub_category'])) {
            echo '<ul>';
            buildTree($node['sub_category']);
            echo '</ul>';
        }
    }
}

function categoryTree($db, $root_id) {
    $tree = array();
    $sub_categories = $db->get_results('SELECT * FROM categories WHERE parentid="'.$root_id.'"');
    if ($sub_categories) {
        foreach ($sub_categories as $sub_category) {
             $tree[] = array(
                 "id" => $sub_category->id,
                 "title" => $sub_category->title,
                 "sub_category" => categoryTree($db, $sub_category->id)
            );
        }
    }
    return $tree;
}

1 个答案:

答案 0 :(得分:1)

它已经解决了。这是我的新代码:

function buildTree($db, $tree) {
    foreach ($tree as $node)
    {
        $header = array();
        $id = $node['id'];
        $can_i_stop = false;
        $has_parent = $db->get_var('SELECT parentid FROM categories WHERE id="'.$node['id'].'"');
        if (!($has_parent == '') && !($has_parent == null)) {
            $seperator = ' > ';
        }else{
            $seperator = '';
        }
        while ($can_i_stop == false) {
            $parent_id = $db->get_var('SELECT parentid FROM categories WHERE id="'.$id.'"');
            if (!($parent_id == '') && !($parent_id == null)) {
                $parent_title = $db->get_var('SELECT title FROM categories WHERE id="'.$parent_id.'"');
                array_push($header, $parent_title);
                $id = $parent_id;
                $can_i_stop = false;
            }else{
                $can_i_stop = true;
            }
        }
        echo '<li>'.implode(' > ', array_reverse($header)).$seperator.$node['title'].'</li>';
        if (!empty($node['sub_category'])) {
            echo '<ul>';
            buildTree($db, $node['sub_category']);
            echo '</ul>';
        }
    }
}

function categorySubTree($db, $root_id) {
    $tree = array();
    $sub_categories = $db->get_results('SELECT * FROM categories WHERE parentid="'.$root_id.'"');
    if ($sub_categories) {
        foreach ($sub_categories as $sub_category) {
            $tree[] = array(
                "id" => $sub_category->id,
                "title" => $sub_category->title,
                "sub_category" => categorySubTree($db, $sub_category->id)
            );
        }
    }
    return $tree;
}

function categoriesTree($db) {
    $tree = array();
    $categories = $db->get_results('SELECT * FROM categories WHERE is_subcategory=0');
    if ($categories) {
        foreach ($categories as $category) {
            $tree[] = array(
                "id" => $category->id,
                "title" => $category->title,
                "sub_category" => categorySubTree($db, $category->id)
            );
        }
    }
    return $tree;
}