Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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");
            }
        }
share|improve this question
    
Try $html[index_of_your_array] in push. like array_push ($html_doc[1], $valueToPush); –  user3520434 May 28 at 17:38
1  
how you want the output look like –  Feroz Akbar May 28 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 –  Dinizworld May 28 at 18:11
    
@Dinizworld You should just take your code in the comments & place it in the question. –  JakeGould May 28 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.