【问题标题】:populating multidimensional array from mysql hieararchy table to php将多维数组从mysql层次结构表填充到php
【发布时间】:2012-09-10 14:32:15
【问题描述】:

我直接回答这个问题。

我有一个表,有 3 列:“id”、“name”和“parent”。 每个id代表类别,parent是引用子类别的id。

我需要构建一个菜单,即一个无序列表和嵌套无序列表。 我得出的结论是,我必须将其转换为数组,是否有另一种仅使用 mysql 的方法?如果没有,你能告诉我在 php 中构建多维数组的技术吗?

【问题讨论】:

标签: php mysql


【解决方案1】:

我想出了另一个不使用递归的代码:

<?php
//Let's say the DB returns:
$categories = array(
    array( 'id' => 1, 'name' => 'Category 1', 'parent' => null ),
    array( 'id' => 2, 'name' => 'Category 2', 'parent' => null ),
    array( 'id' => 3, 'name' => 'Category 3', 'parent' => 1 ),
    array( 'id' => 4, 'name' => 'Category 4', 'parent' => 3)
    );


$sortedCategories = assignChildren( $categories );

function assignChildren( &$categories )
{
    $sortedCategories = array();
    foreach( $categories as &$category )
    {
        if ( !isset( $category['children'] ) )
        {
            // set the children
            $category['children'] = array();
            foreach( $categories as &$subcategory )
            {
                if( $category['id'] == $subcategory['parent'] )
                {
                    $category['children'][] = &$subcategory;
                }
            }
        }

        if ( is_null( $category['parent'] ) )
        {
            $sortedCategories[] = &$category;
        }

    }

    return $sortedCategories;
}

var_dump( $sortedCategories );

输出:

array(2) {
  [0]=>
  &array(4) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(10) "Category 1"
    ["parent"]=>
    NULL
    ["children"]=>
    array(1) {
      [0]=>
      &array(4) {
        ["id"]=>
        int(3)
        ["name"]=>
        string(10) "Category 3"
        ["parent"]=>
        int(1)
        ["children"]=>
        array(1) {
          [0]=>
          &array(4) {
            ["id"]=>
            int(4)
            ["name"]=>
            string(10) "Category 4"
            ["parent"]=>
            int(3)
            ["children"]=>
            array(0) {
            }
          }
        }
      }
    }
  }
  [1]=>
  &array(4) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(10) "Category 2"
    ["parent"]=>
    NULL
    ["children"]=>
    array(0) {
    }
  }
}

【讨论】:

    【解决方案2】:

    其中一种方法是准备您的多维数组,如下所示......它可能不是完美的,但它对我来说效果很好......

    $result_category = mysql_query('select all records query here ...');
        $categoryData = array(
        'items' => array(),
        'parents' => array()
    );
    
    while ($categoryItem = mysql_fetch_assoc($result_category))
    {
        $categoryData['items'][$categoryItem['category_id']] = $categoryItem;
        $categoryData['parents'][$categoryItem['parent_id']][] = $categoryItem['category_id'];
    }
    

    【讨论】:

    • 是的,但是如果孩子自己也有孩子呢?他将如何呈现他的列表?
    • @Bgi 'parents' 数组将包含对所有父子关系的所有引用
    • 好的,但是如何渲染列表呢?使用数组搜索?
    • 您可以呈现列表、构建下拉列表、无序列表或者您想要呈现数据...这将是一个 2 步过程 1. 你准备上面的数组 2. 你循环遍历每个项目,最好在单独的函数/方法中执行,然后构建您的列表。
    【解决方案3】:

    您必须进行一次数据库调用才能获得所有类别的列表。

    然后您必须使用递归函数为每个类别分配其子类别,并为每个子类别分配其子类别,并一次又一次地分配(感谢递归,这很“容易”)......

    【讨论】:

    • 第一个很简单,我已经知道了。 :-) 第二部分让我思考。
    猜你喜欢
    • 1970-01-01
    • 2013-11-05
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2013-06-25
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多