Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对象数组中的PHP推送数组_Php_Arrays_Object_Echo - Fatal编程技术网

对象数组中的PHP推送数组

对象数组中的PHP推送数组,php,arrays,object,echo,Php,Arrays,Object,Echo,与我先前的问题有关 我现在有这种情况: $html_doc = (object) array ( "css" => array(), "js" => array(), "body" => array ( array ( "page" => "error"

与我先前的问题有关

我现在有这种情况:

$html_doc = (object) array
    (
    "css"       => array(),
    "js"        => array(),
    "body"  => 
        array
            (
            array
               (
               "page"   => "error"
               ),
            array
               (
               "controller" => "adminpanel"
               ),
            ),  
    );
通过foreach构造,我得到了我想要的,当body键是一个页面时,我可以做我想要的,当body键也是一个控制器时

但如果我这样开始:

$html_doc = (object) array
    (
    "css"       => array(),
    "js"        => array(),
    "body"      => array()
    );
问题1 如果可能的话,我如何在这之后添加一行“我更喜欢”带有值错误的键页,例如在代码页中带有值联系人的其他位置?当然,我尝试了很多符合逻辑的事情,但没有结果

问题2 就像我上面所说的,使用foreach我得到了我想要的,但是如果我想用keyindex 0来回传例如body,我该怎么做呢?例如,我试过:

 echo $html_doc->body[0]["controller"];
但是我没有把它展示出来。只有当我打开foreach循环时。这次我做错了什么

编辑@Feroz-Akbar

在评论中回答你的问题并不是一个很大的成功,我不知道加价的效果与这里不同,我只有5分钟来编辑我的评论并找出答案。我现在的输出是:

    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");
            }
        }

在push中尝试$html[index_of_your_array]。与array\u push$html\u doc[1]类似,$valueToPush;您希望输出的样子达到$html\u doc->body as$location=>$body\u content{if$location===controller include\u PAGES\u DIR.$html\u doc->controller./php/$body\u content.\u body.php;其他包括\u WEBSITE\u DIR\uu.body/$location./.$body\u content.\u body.php;}请原谅,我不知道我怎样才能在考试中加分comments@Dinizworld你应该把你的代码放在注释中,然后把它放在问题中。