0

I have an array like this

'contents' => array(
              'row' => array(
                    'col-xs-6' => 'grid 1',
                    'col-xs-6' => 'grid 2'
              ),
              'row' => array(
                    'col-xs-6' => 'grid 3',
                    'col-xs-6' => 'grid 4'
              )
        )

Now I want to get the key from the array but failed, I have try with this code

{% for key, values in contents %}
     <div class="{{ key }}">
          {% for klass, contain in values %}
                 <div class="{{ klass }}">
                      {{ contain }}
                 </div>
          {% endfor %}
     </div>
{% endfor %}

Output:

<div class="row"><div class="col-xs-6">grid 4</div></div>

I don't know why it only appear once, but if I try changing the key name with different value and it works. Please help me. Thank you.

4
  • Did @Thamilan's answer provide you with the answer you needed? Or do you just want to have "row" appear as the 1st class in the 1st div tag, and "col-xs-6" in the 2nd div tag, and then the name within the 2nd div being "grid 1"... If so, there might be an easier answer. Or maybe your contents array is fixed? Commented Dec 27, 2016 at 6:09
  • @AlvinBunk Yes, he did but he not give me a solution, so I can't accept it as an answer. And yes, I want 'row' as the 1st class and 'col-xs-6' as the 2nd class in the 2nd div tag. So, how I can displays it as what the array looks like regardless array name conflict ? Commented Dec 27, 2016 at 14:57
  • I was thinking you could use Twig's length function on the array, but that probably only works for a valid PHP array. Is there a way to pass in the number of grid values as a number variable? If so, there's a better answer. Let me know. Commented Dec 27, 2016 at 17:11
  • @AlvinBunk Yes it is. Please let me know. Commented Dec 28, 2016 at 3:37

2 Answers 2

4

PHP array should have unique keys.

In your case,

  1. First row gets replaced by second row
  2. First col-xs-6 in the second row will be replaced by second col-xs-6

So, your resultant array would be:

'contents' => array(
      'row' => array(
            'col-xs-6' => 'grid 4'
      )
)
Sign up to request clarification or add additional context in comments.

Comments

1

Based on your comments:

So let's say you pass in a variable called gridCount, then in Twig you can code like so:

{% for i in 0..gridCount %}
    <div class="row"><div class="col-xs-6">grid {{ i }}</div></div>
{% endfor %}

Let me know if you also plan to change the class values. In the above, I presume you don't need to. But if they are also dynamic, then you need to tell me how they change.

2 Comments

Good idea, maybe I don't need the class values because i can divided it by 12 and make the result as class. Thank you !
You are welcome! Please post another question if you need further help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.