I have the following Arrays
$elemento =
["title"=> //Level 1
["iSh*t","text-center",40,"red"] //Level 2
];
$elemento2 =
["row"=>[ //Level 1
["col"=>[ //Level 2
["sm-6"=>[ //Level 3
["text"=> //Level 4
["Lorem ipsum sit amet","text-center",16,"blue"] //Level 5
]
]
],
["sm-6"=>[ //Level 3
["text"=> //Level 4
["Lorem ipsum sit amet","text-center",16,"blue"] //Level 5
]
]
],
["sm-6"=>[ //Level 3
["row"=>[ //Level 4
["col"=>[ //Level 5
["sm-6"=>[ //Level 6
["title" => //Level 7
["Lorem ipsum sit amet","text-center",16,"blue"] //Level 8
]
]
]
]
],
["col"=>[ //Level 5
["sm-6"=>[ //Level 6
["title" => //Level 7
["Lorem ipsum sit amet","text-center",16,"blue"] //Level 8
]
]
],
["sm-6"=>[ //Level 6
["title" => //Level 7
["Lorem ipsum sit amet","text-center",16,"blue"] //Level 8
]
]
]
]
]
]
]
]
]
]
]
]
];
I want to create a function than iterates on every "level" of my array no matter how many "levels".
Now, I have a function but, yes I know it's not optimized, but I just wanna see how can I make this work.
public function toHTML($superobj,$html){
$html = "<div class=\"bloque\"><div class=\"c-c\">";
XO::_html_get($superobj,$html);
$html = $html."</div></div>";
return $html;
}
private function _html_get($superobj,&$html){
foreach ($superobj as $i => $val) {
foreach ($val as $i => $val) {
switch ($i) {
case 'title':
$html = $html."<h1 class=\"".$val[1]."\" style=\"font-size:".$val[2]."px;color:".$val[3]."\">".$val[0]."</h1>";
break;
case 'text':
$html = $html."<p class=\"".$val[1]."\">".$val[0]."</p>";
break;
case 'image':
$html = $html."<img src=\"".$val[0]."\" class=\"".$val[1]."\">";
break;
case 'row':
$html = $html."<div class=\"row\">";
foreach ($val as $i => $val) {
foreach ($val as $i => $val) {
if ($i == "columna") {
foreach ($val as $i => $val) {
$html = $html."<div class=\"col-".array_keys($val)[0]."\">";
foreach ($val as $i => $val) {
foreach ($val as $i => $val) {
foreach ($val as $i => $val) {
if ($i == "texto") {
$html = $html."<p class=\"".$val[1]."\">".$val[0]."</p>";
}
}
}
}
$html = $html."</div>";
}
}
}
}
$html = $html."</div>";
break;
case 'row':
$html = $html."";
break;
case 'div':
$html = $html."";
break;
case 'button':
$html = $html."";
break;
case 'link':
$html = $html."";
break;
default:
# code...
break;
}
}
}
}
Now, "working" is, but if I add more levels to level 3 (sm-6) won't work cause the function is not prepared for that..
I know this way is not optimized and/or recommended, but...
How can I do this? :)
If you wanna recommend me other way to do this, please don't recommend me frameworks, even if that makes the work and tasks easy, I will not learn anything, I love "hand" code.