我有一个多维数组,我想将其转换为修改后的预序遍历树:
$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")
);
我尝试过遍历嵌套数组或使用迭代器,但我只是没有成功获得正确的界限。
在这里,我有什么要被左约束的,我如何得到正确的?
foreach ($data as $groupname => $group) {
echo $i.':'.$groupname . '<br/>';
if (is_array($group)) {
foreach ($group as $blockname => $block) {
$i++;
if (is_array($block)) {
echo " " .$i.':'. $blockname . '<br/>';
foreach ($block as $setname) {
$i++;
echo " " .$i.':'. $setname . '<br/>';
}
} else {
echo " " .$i.':'. $block . '<br/>';
}
}
}
$i++;
}