简体   繁体   English

如何在ZF2“树枝”视图中表示PHP数组

[英]How to represent a PHP array in a ZF2 “twig” view

In my Service I am executing this query to find out the # of prayer requests in each prayer request category: 在我的服务中,我正在执行以下查询以找出每个祈祷请求类别中的祈祷请求数量:

SELECT a0_.prayer_request_category_reference AS prayer_request_category_reference_0, COUNT(a0_.reference) AS sclr_1 FROM acts2301_prayer.pr a0_ GROUP BY a0_.prayer_request_category_reference 

Then I assign the results to an array: 然后将结果分配给数组:

if (is_array($result) && count($result) > '0') {
    foreach ($result AS $value) {
        $cateogry_request_count[$value['prayerRequestCategoryReference']] = $value['category_request_count'];
    }
}

The results of " print_r( $cateogry_request_count ) " are “ print_r($ cateogry_request_count)”的结果是

 Array
 {
     [1] => 5
     [2] => 5
     [3] => 5
     [4] => 2
     [5] => 1
     [8] => 3
 )

I am unsure of how to represent this array within my Twig view. 我不确定如何在Twig视图中表示此数组。 I am displaying this within the context of a loop. 我在循环的上下文中显示它。 This variable in my service " $value['prayerRequestCategoryReference'] " equals " value.reference " in the loop within my Twig. 我的服务中的此变量“ $ value ['prayerRequestCategoryReference']”等于Twig中循环中的“ value.reference”。

The following are my unsuccessful attempts to present category_request_count.# 以下是我未能成功提交category_request_count的尝试。#

Try #1: 尝试#1:

 {% set yourNewVarName = category_request_count.value.reference %}
 {{ yourNewVarName|escape }}

Try #2: 尝试#2:

 {{ 'category_request_count' . value.reference }}

Try #3: 尝试#3:

 {{ category_request_count . value.reference }}

According with the documentation , to access a "dynamic" attribute of a variable you can do 根据文档 ,您可以访问变量的“动态”属性

{{ category_request_count[value.reference] }}

or using the attribute function 或使用属性功能

{{ attribute(category_request_count, value.reference) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM