Search code examples
phpmultidimensional-arraymptt

PHP converting multidimensional array to Modified Pre-order Traversal Tree


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

Solution

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