Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.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 如何在XML模板上处理多维数组?_Php_Arrays_Xml_Multidimensional Array_Twig - Fatal编程技术网

Php 如何在XML模板上处理多维数组?

Php 如何在XML模板上处理多维数组?,php,arrays,xml,multidimensional-array,twig,Php,Arrays,Xml,Multidimensional Array,Twig,这是我的代码。。。我在我的项目中使用的 $app->post( '/chk_db', function () use ($app){ require_once 'lib/mysql.php'; $dx = connect_db('MyPhotos'); //XML RESPONSE $app->response->setStatus(0); $res = $app->response();

这是我的代码。。。我在我的项目中使用的

$app->post(
  '/chk_db',
    function () use ($app){
      require_once 'lib/mysql.php';
      $dx = connect_db('MyPhotos');

      //XML RESPONSE
      $app->response->setStatus(0);
      $res = $app->response();
      $res['Content-Type'] = 'application/xml';
      $view = $app->view();
      $view->setTemplatesDirectory('./');
      $oArray = array("Status"=> $dx.status, "code" => $dx.code);
      return $app->render('chkdb.xml', $oArray);
  }
);
因此,我将此数组作为xml模板的输入 (顺便说一下……这是json_编码的……我只是用它来表示数组……谢谢……)

如何在模板上处理它们?chk_db.xml

{% for x in ????%}
<MyPhotos>
<ObjID>{{x.ObjID}}</ObjID>
...
<inode>{{x.inode}}</inode>
</MyPhotos>
{% else %}
<data>No data Found</data>
{% endfor %}
{%x在???%}
{{x.ObjID}
...
{x.inode}
{%else%}
没有找到任何数据
{%endfor%}

谢谢…

您应该为这个数组命名。只需重写
$app->render
部分,如下所示:

return $app->render('chkdb.xml', ['photos' => $oArray]);
并在模板中写入:

{% for x in photos %}

哦,老兄。。。我的错。。我找到了答案。。。我的模板上没有任何错误。。不给我发送的数组命名是我的错。。这是正确的代码

$app->post(
'/get_gallery_allphotos',
    function () use ($app) {
        require_once 'lib/mysql.php';
        $re = getAll_photos('MyPhotos');
        $app->response->setStatus(200);
        $app->response()->headers->set('Content-Type', 'text/xml');
        return $app->render('myphotos_allphotos_admin.xml', array("ArrayName" => $re));
    }

);
我忘了命名我发送的数组。。。所以在这里我把ArrayName作为$re的名称

因此,在模板中,我使用

{% for x in ArrayName %}
<MyPhotos>
    <ObjID>{{x.ObjID}}</ObjID>
    <ParenetID>{{x.ParenetID}}</ParenetID>
    <Path>{{x.Path}}</Path>
    <Title>{{x.Title}}</Title>
    <ChildCount>{{x.ChildCount}}</ChildCount>
    <Owner>{{x.Owner}}</Owner>
    <Comment>{{x.Comment}}</Comment>
    <inode>{{x.inode}}</inode>
</MyPhotos>
{% else %}
    Not Found
{% endfor %}
{%x在ArrayName%}
{{x.ObjID}
{{x.ParenetID}}
{x.Path}
{{x.Title}}
{x.ChildCount}
{{x.Owner}
{{x.Comment}}
{x.inode}
{%else%}
找不到
{%endfor%}

希望它能帮助别人^ ^,

刚刚也找到了。。无论如何,谢谢D
{% for x in ArrayName %}
<MyPhotos>
    <ObjID>{{x.ObjID}}</ObjID>
    <ParenetID>{{x.ParenetID}}</ParenetID>
    <Path>{{x.Path}}</Path>
    <Title>{{x.Title}}</Title>
    <ChildCount>{{x.ChildCount}}</ChildCount>
    <Owner>{{x.Owner}}</Owner>
    <Comment>{{x.Comment}}</Comment>
    <inode>{{x.inode}}</inode>
</MyPhotos>
{% else %}
    Not Found
{% endfor %}