1

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.

4
  • 3
    You can use recursion lornajane.net/posts/2012/… Commented Jun 10, 2017 at 13:45
  • Yes, I was wonder myself about it. :) Thank you! Commented Jun 10, 2017 at 13:53
  • It would be nice if you come back here and present your recursive solution for others to learn. Commented Jun 10, 2017 at 17:56
  • @ArturR.Czechowski I'm just learning about recursive functions but is not easy anyway. It depends on which situation you need. Commented Jun 12, 2017 at 3:48

1 Answer 1

1

A very simplified structure for what you are trying to achieve is:

function recursiveIteration ($array, &$html){

    foreach ($array as $key => $value){

        if (is_array($value)){
            recursiveIteration($value, $html);
        }
        else {
            $html .= intendedHtmlForThisItem($key);
        }
    }
}

Which allows to to create as many levels as you want.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but there's some array keys to take, like "text","row","col", and "sm-6" for those the result is diferent. :/
This is just a basic idea of how to do it recursively. If you need to have some keys treated specially you can still have in if/switch for them at the top of the loop, and fall back to the recursive if otherwise
Yea, I'm looking for the best performance posible on hand code. I know it's possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.