phalcon-如何循环通过关联(数组)模型:: find();输出键 [英] phalcon - how to loop through associative (array) model::find(); to output keys

查看:364
本文介绍了phalcon-如何循环通过关联(数组)模型:: find();输出键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历model :: find结果的列. 我认为可能的是将返回的对象强制转换为数组,以便能够遍历各列,但这是行不通的.

I want to loop through the columns of the model::find results. What I thought was possible is to cast the returning object to an array to be able to loop throught the columns, but that does not work.

这是我的 控制器代码:

Here is my Controller code:

<?php
class ManageController extends ControllerBase
{
    public function indexAction()
    {
        $this->view->setVar("pages",(array) Pages::find());
    }
}

并查看代码:

    {% for key,value in pages %}
    <p>key: {{key}}</p>
    {% endfor%}

任何帮助都是有用的

推荐答案

使用它;

<?php
class ManageController extends ControllerBase
{
    public function indexAction()
    {
        $this->view->setVar("pages", Pages::find());
    }
}

并查看代码:

{% for page in pages %}
   {# in this case the key is just "0,1,2,3..." #}
   {# so we use the loop index (or loop.index0 for zero based) #}
   <p>This is the page #{{ loop.index }}</p>
   <p>{{ page.title }}</p>
{% endfor%}


但是,如果您确实还需要遍历各个键,请使用:


But if you really need to loop through the keys too, use:

{% for key, value in items %}
    Key: {{ key }}
    Value: {{ value }}
{% endfor%}

这篇关于phalcon-如何循环通过关联(数组)模型:: find();输出键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆