2016-09-13 42 views
0

私はキー名(mysqlデータから列名)を出力するようにtwigをしようとしています。 は、私は何をしたいbasiclyです:<a class="listquestions" href="#" id="{{ key }}"... ループのためのtwigで配列値のキー名を取得

現在のコードベース:

<table id="listquestions" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%"> 
<thead> 
    <tr> 
     {% for key, answer in answers[0] %} 
      <th>{{ key }}</th> 
     {% endfor %} 
    </tr> 
</thead> 
<tbody> 
    {% for key,answer in answers %} 
     <tr> 
      <td>{{ answer.a_id }}</td> 
      <td> 
       <a class="listquestions" href="#" data-name="a_text" data-type="text" data-pk="{{ answer.a_id }}" data-url="{{ path_for('qa.edit') }}" data-title="enter attribute name"> 
        {{ answer.a_text }} 
       </a> 
      </td> 
      <td> 
       <a class="listquestions" href="#" data-name="a_attribute_name" data-type="text" data-pk="{{ answer.a_id }}" data-url="{{ path_for('qa.edit') }}" data-title="enter attribute name"> 
        {{ answer.a_attribute_name}} 
       </a> 
      </td> 
     </tr> 
    {% endfor %} 
</tbody> 

PHP関数でvar_export($データ、真の)出力:

array (
    0 => 
    array (
    'a_id' => '1', 
    'a_text' => 'text', 
    'a_attribute_name' => 'attr', 
), 
    1 => 
    array (
    'a_id' => '2', 
    'a_text' => 'text', 
    'a_attribute_name' => 'attr', 
), 
    2 => 
    array (
    'a_id' => '3', 
    'a_text' => 'text', 
    'a_attribute_name' => 'attr', 
), 
) 

私が追加しようとしましたkey($answer.a_text)ではなく、key()であるTwigExtensionは、twig for-loopsでは機能しません。

私は何が欠けていますか?私は<thead>の中にキー名を出力することができますが、私は2番目のfor-loopでこれをしたいと思います。

+0

問題は何ですか? –

+0

ネストされた配列と何か関係があると思いますが、私はの$ arr [0]を反復処理しています。 '{{key }} '' 0 '、' 1 '、' 2 '...'を出力するだけです。 –

+0

'key'の期待される内容は?あなたは手動でキーを指定する必要があるでしょう – DarkBee

答えて

1
<table id="listquestions" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%"> 
<thead> 
    <tr> 
     {% for key, answer in answers[0] %} 
      <th>{{ key }}</th> 
     {% endfor %} 
    </tr> 
</thead> 
<tbody> 
    {% for key,answer in answers %} 
     {% for field, value in answer %} 
     <tr> 
      {% if field == 'a_id' %} 
      <td>{{ answer.a_id }}</td> 
      {% else %} 
      <td> 
       <a class="listquestions" href="#" data-name="{{ field }}" data-type="text" data-pk="{{ answer.a_id }}" data-url="path_for('qa.edit')" data-title="enter attribute name"> 
        {{ value }} 
       </a> 
      </td> 
      {% endif %} 
     </tr> 
     {% endfor %} 
    {% endfor %} 
</tbody> 
</table> 
関連する問題