1

Related to my previous question PHP push new key and value in existing object array

I have now this situation:

$html_doc = (object) array
    (
    "css"       => array(),
    "js"        => array(),
    "body"  => 
        array
            (
            array
               (
               "page"   => "error"
               ),
            array
               (
               "controller" => "adminpanel"
               ),
            ),  
    );

With a foreach construct I get what I want, when the body key is a page I can do what I want and when the body key is a controller also.

But if I start like this:

$html_doc = (object) array
    (
    "css"       => array(),
    "js"        => array(),
    "body"      => array()
    );

question 1 How can I add (I prefer one line if possible) after this a key page with value error and for example at an other place in my code page with value contact? Ofcourse I tried a lot things what looks for me logical, but no result.

question 2 Like I said above, with a foreach I get what I want, but if I want to echo for example body with keyindex 0, how do I do that? I tried for example:

 echo $html_doc->body[0]["controller"];

But I dont get it displayed. Only when I the foreach loop. What am I doing this time wrong?

edit @Feroz Akbar

Answering your question in comments wasn't a big success, I didn't know the mark-up works different then here, and I had only 5 minutes to edit my comment and to find out. My output now is this:

    if (isset($html_doc->body))
        {
        foreach ($html_doc->body as $location => $body_content) 
            {
            if ($location === "controller")
                include(_PAGES_DIR_ . $html_doc->controller . "/php/" . $body_content . "_body.php");
            else
                include(_WEBSITE_DIR_ . "body/" . $location . "/" . $body_content . "_body.php");
            }
        }
3
  • Try $html[index_of_your_array] in push. like array_push ($html_doc[1], $valueToPush); Commented May 28, 2014 at 17:38
  • 1
    how you want the output look like Commented May 28, 2014 at 17:46
  • foreach ($html_doc->body as $location => $body_content) { if ($location === "controller") include(_PAGES_DIR_ . $html_doc->controller . "/php/" . $body_content . "_body.php"); else include(_WEBSITE_DIR_ . "body/" . $location . "/" . $body_content . "_body.php"); } forgive me, I don't know how I can mark-up in comments Commented May 28, 2014 at 18:11

0

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.