PHP converting multidimensional array to Modified Pre-order Traversal Tree

二次信任 提交于 2019-12-12 01:49:52

问题


I have a multidimendional array that I want to transform to a Modified Pre-order Traversal Tree:

$data = array(
    "MTGO" => array("Magic Online" => array("MTGO Masters Edition", "MTGO Masters Edition II", "MTGO Masters Edition III", "MTGO Masters Edition IV")),
    "Expansions" => array("Ice Age Cycle" => array("Coldsnap Theme Decks", "Ice Age", "Alliances"), "Theros" => array("Theros")),
    "unclassified" => array("Portal", "Eight Edition")
);

I've tried by looping through the nested arrays, or using iterators, but I just didn't succeeded in getting the right bound.

Here what I have for getting left bound, how do I get the right one ??

foreach ($data as $groupname => $group) {
    echo $i.':'.$groupname . '<br/>';
    if (is_array($group)) {
        foreach ($group as $blockname => $block) {
            $i++;
            if (is_array($block)) {
                echo "&nbsp;&nbsp;&nbsp;" .$i.':'. $blockname . '<br/>';
                foreach ($block as $setname) {
                    $i++;
                    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .$i.':'. $setname . '<br/>';
                }
            } else {
                echo "&nbsp;&nbsp;&nbsp;" .$i.':'. $block . '<br/>';
            }
        }
    }
    $i++;
}

回答1:


Finally I've found a solution, for my needs so it goes 3 level deep, but it can be easily adapted to recursive needs with some tweaks:

class Mptt_object {
    public $lb;
    public $rb;
    public $data;
    public function __construct($name, $lb, $rb = null) {
        $this->data = $name;
        $this->lb = $lb;
        $this->rb = $rb;
    }
}


function getMptt(array $data)
{
    /** @var Mptt_object[] $mptts */
    $mptts = array();

    $i = 0;
    foreach ($data as $firstLevelElementKey => $firstLevelElement) {

        $i++;

        $firstLevelElementMptt = new Mptt_object($firstLevelElementKey, $i);

        foreach ($firstLevelElement as $secondLevelElementKey => $secondLevelElement) {
            if (is_array($secondLevelElement)) {

                $i++;
                $secondLevelElementMptt = new Mptt_object($secondLevelElementKey, $i);

                $thirdLevelElementMptts = array();
                foreach ($secondLevelElement as $thirdLevelElementKey => $thirdLevelElement) {
                    $i++;
                    $thirdLevelElementMptt = new Mptt_object($thirdLevelElement, $i);
                    $i++;
                    $thirdLevelElementMptt->rb = $i;
                    $mptts[] = $thirdLevelElementMptt;
                }
                $i++;
                $secondLevelElementMptt->rb = $i;
                $mptts[] = $secondLevelElementMptt;
                $mptts = array_merge($mptts, $thirdLevelElementMptts);
            } else {
                $i++;
                $secondLevelElementMptt = new Mptt_object($secondLevelElement, $i);
                $i++;
                $secondLevelElementMptt->rb = $i;
                $mptts[] = $secondLevelElementMptt;
            }
        }
        $i++;
        $firstLevelElementMptt->rb = $i;
        $mptts[] = $firstLevelElementMptt;
    }
    return $mptts;
}


来源:https://stackoverflow.com/questions/20805143/php-converting-multidimensional-array-to-modified-pre-order-traversal-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!