我有一组用于在表格中打印的值
array(4) {
["ayel.bokbok"]=>
array(1) {
["/app_dev.php/index/"]=>
int(2)
}
["ryhel1120"]=>
array(2) {
["/app_dev.php/index/"]=>
int(47)
["/app_dev.php/users/"]=>
int(84)
}
["ariel.alvarez"]=>
array(1) {
["/app_dev.php/users/"]=>
int(3)
}
["rainercedric23"]=>
array(3) {
["/app_local.php/admin/analytics"]=>
int(338)
["/app_local.php/index/"]=>
int(7)
["/app_local.php/users/"]=>
int(13)
}
如何在twig中打印ayel.bokbok
因为在此代码中{%key,value in foo%}
twig它只打印索引而不是键的键名。
@ponciste这是{{dump(array)}}在树枝上打印的内容:
array(1){
[0] => array(4)
{ ["ariel.alvarez"]=> array(2) { ["/app_dev.php/index/"]=> int(1) ["/app_dev.php/users/"]=> int(3) } ["ayel.bokbok"]=> array(1) { ["/app_dev.php/index/"]=> int(4) } ["ryhel1120"]=> array(2) { ["/app_dev.php/index/"]=> int(58) ["/app_dev.php/users/"]=> int(121) } ["rainercedric23"]=> array(3) { ["/app_local.php/admin/analytics"]=> int(312) ["/app_local.php/index/"]=> int(7) ["/app_local.php/users/"]=> int(8) } } }
这是来自控制器的代码:
<?php
namespace foo\BarBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AnalyticsController extends Controller {
public function indexAction() {
$Gapi = $this->container->get('bti_rep_sys.gapiManager')->GapiGetOverview();
$Gapifiltered = $this->container->get('bti_rep_sys.gapiManager')->GapiGetFilter();
return $this->render('fooBarBundle:Analytics:index.html.twig', array('gapidata' => $Gapi, 'gapidatafiltered'=> $Gapifiltered));
}
}
这是我的index.html.twig代码:
{%extends "fooBarBundle::layout.html.twig"%}
{%block body%}
{%include "fooBarBundle:Analytics:GoogleCharts.js.twig"%}
<h1>Analytics Report </h1>
<div class="row">
<div class="float-left" id="chart_div_bar"></div>
<div class="float-right" id="chart_div_pie"></div>
</div>
<div class="right-align">(Updated as of {{"now"|date('d/m/Y')}})</div>
{{ dump(gapidatafiltered) }} // for dumping of data
{%endblock%}
答案 0 :(得分:3)
如果您正在尝试获取/app_dev.php/index/
字符串,那么这可能会起到作用
{% for route,paths in foo %}
{% for path in paths|keys %}
{{ path }}
{% endfor %}
{% endfor %}
答案 1 :(得分:0)
您可以使用key过滤器获取数组键:
{% for key in foo|keys %}
Key: {{ key }}
{% endfor %}
但是,正如@ponciste所说,请求告诉我们{{ dump(foo) }}
。
答案 2 :(得分:0)
我通过在获取密钥名称之前创建for循环语句找到了解决方案:
{% for array in gapidatafiltered %}
{% for user, key in array%}
{{user}}
{%for x,y in key%}
{{x}}{{y}}
{%endfor%}
{% endfor %}
{% endfor %}
这将获取他们所需的所有键,特别是路径中具有int值的所有用户名和路径。干杯
答案 3 :(得分:0)
我相信这样做,一气呵成。
{% for item,key in data|keys %}
<li><a href="{{ key }}">{{ item }}</a></li>
{% endfor %}