关联数组到嵌套集

时间:2012-05-07 14:10:10

标签: php nested-sets

我正在尝试获取关联数组的嵌套集数据。该数组需要存储为mysql数据库中的嵌套集。数组我想得到嵌套的数据: 嵌套集实现的链接 http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

Array
(
    [NEOPLASMS OF MATURE T CELLS OR NK CELLS] => Array
        (
            [Adult T-cell leukemia/lymphoma] => Array
                (
                    [1] => Helper T cell
                    [2] => HTLV-1 provirus present in tumor cells
                    [3] => Adults with cutaneous lesions, marrow involvement, and hypercalcemia; occurs mainly in Japan, West Africa, and the Caribbean; aggressive
                )

            [Peripheral T-cell lymphoma, unspecified] => Array
                (
                    [4] => Helper or cytotoxic T cell
                    [5] => No specific chromosomal abnormality
                    [6] => Mainly older adults; usually presents with lymphadenopathy; aggressive
                )

            [Anaplastic large-cell lymphoma] => Array
                (
                    [7] => Cytotoxic T cell
                    [8] => Rearrangements of ALK
                    [9] => Children and young adults, usually with lymph node and soft-tissue disease; aggressive
                )

            [Extranodal NK/T-cell lymphoma] => Array
                (
                    [10] => NK-cell (common) or cytotoxic T cell (rare)
                    [11] => EBV-associated; no specific chromosomal abnormality
                    [12] => Adults with destructive extranodal masses, most commonly sinonasal; aggressive
                )

            [Mycosis fungoides/Sézary syndrome] => Array
                (
                    [13] => Helper T cell
                    [14] => No specific chromosomal abnormality
                    [15] => Adult patients with cutaneous patches, plaques, nodules, or generalized erythema; indolent
                )

            [Large granular lymphocytic leukemia] => Array
                (
                    [16] => Two types: cytotoxic T cell and NK cell
                    [17] => No specific chromosomal abnormality
                    [18] => Adult patients with splenomegaly, neutropenia, and anemia, sometimes, accompanied by autoimmune disease
                )

        )

)

我想要的数组输出:

Array
(
    [0] => Array
        (
            [0] => NEOPLASMS OF MATURE T CELLS OR NK CELLS
            [1] => 1
            [2] => 26
        )

    [1] => Array
        (
            [0] => Adult T-cell leukemia/lymphoma
            [1] => 2
            [2] => 9
        )

..............等等。

以下是我被困在的地方:

    class nested_data
{
    var $nc = 1;
    var $map;
    var $nest = array();

    function make_nest()
    {
        array_walk($this->map, array($this,"fetch_nest_data"));
        foreach($this->map as $map)
        {
            array_walk($map,array($this,"fetch_nest_data"));
        }

    }



    function fetch_nest_data($val,$key)
    {
        $content = (is_array($val)) ? $key : $val;
        $lft = $this->nc++;
        $rgt= (is_array($val)) ? (count($val, COUNT_RECURSIVE) + $this->nc) : $this->nc++;
        $this->nest[]=array($content,$lft,$rgt);
    }



}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我所做的是创建一个嵌套的set管理器,它具有addChild(),addSibling(),moveNodeUp(),moveNodedown(),setNodePosition()等方法或函数。从那里你可以循环遍历一个数组,文本文件表示,无论如何,并很快将它们变成嵌套集。最困难的部分是打破所有小节点运动。