简体   繁体   中英

associative array to nested set

I am trying to get nested set data for an associative array. The array needs to be stored as a nested set in mysql database. Array i want to get nested set data for: Link for nested set implementation 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 output i want:

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
        )

.............. and so on.

)

Here is where I am stuck at:

    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);
    }



}

Thank you for your help !

What I've done is make a nested set manager which has methods or functions like addChild(), addSibling(), moveNodeUp(), moveNodedown(), setNodePosition(). From there you can just loop through an array, text file representation, whatever and turn them into a nested set pretty quickly. The hardest part is breaking apart all the little node movements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM