1

I have a series of arrays that I need to group together. Here is an example of the incoming data:

[pet_name] => Array
    (
        [0] => Bob
        [1] => Buster
    )

[type] => Array
    (
        [0] => cat
        [1] => dog
    )

[age] => Array
    (
        [0] => 1
        [1] => 22
    )

[gender] => Array
    (
        [0] => female
        [1] => male
    )

I am trying to group these to get the following result:

Pet #1
Name: Bob
Type: Cat
Age: 1
Gender: Female

Pet #2
Name: Buster
Type: Dog
Age: 22
Gender: Male

I've tried the following methods without success:

# Pet Info
{% set total = pet_name|length %}
{% set count = 0 %}

{% for count in total %}
# Pet #{{count + 1}}
* Name: {{ attribute(pet_name, count) }}
* Type: {{ attribute(type, count) }}
* Age: {{ attribute(age, count) }}
* Gender: {{ attribute(gender, count) }}
{% set count = count + 1 %}

{% endfor %}

Second method:

# Pet Info
{% set count = 0 %}

{% for pet in pet_name %}
# Pet #{{count}}
* Name: {{ attribute(pet_name, count) }}
* Type: {{ attribute(type, count) }}
* Age: {{ attribute(age, count) }}
* Gender: {{ attribute(gender, count) }}

{% set count = count + 1 %} 
{% endfor %}

2 Answers 2

2

Try something like this:

{% for key, i in data['pet_name'] %}
  <ul>
      <li>Pet Num: {{ key + 1}}</li>
      <li>{{ data['pet_name'][key] }}</li>
      <li>{{ data['type'][key] }}</li>
      <li>{{ data['age'][key] }}</li>
      <li>{{ data['gender'][key] }}</li>
  </ul>
{% endfor %}

It should print:

<ul>
    <li>Pet Num: 1</li>
    <li>Bob</li>
    <li>cat</li>
    <li>1</li>
    <li>female</li>
</ul>
<ul>
    <li>Pet Num: 2</li>
    <li>Buster</li>
    <li>dog</li>
    <li>22</li>
    <li>male</li>
</ul>

Edit: It seems you have an object with arrays, in that case try something like this:

{% for key, i in data.pet_name %}
  <ul>
      <li>Pet # {{ key + 1}}</li>
      <li>{{ data.pet_name[key] }}</li>
      <li>{{ data.type[key] }}</li>
      <li>{{ data.age[key] }}</li>
      <li>{{ data.gender[key] }}</li>
  </ul>
{% endfor %}

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. Is there a reason you used data? The array I have is not under any other array, so I was curious if data is a twig variable, or by declaring data you are assigning the other values to it?
It's not a twig variable, it's just the name of the variable a passed to the template when I tested it, based on your sample code I'm gonna assume each category is directly available, so remove all data. from my edit, try it like that.
1

Angelcool got me on the right track. Here is the final solution:

{% for key, i in pet_name %}
# #{{key + 1}}
* Name: {{ pet_name[key] }}
* Type: {{ type[key] }}
* Age: {{ age[key] }}
* Gender: {{ gender[key] }}

{% endfor %}

Strange this is that this started to work also:

# Pet Info
{% set count = 0 %}

{% for pet in pet_name %}
# Pet #{{count}}
* Name: {{ attribute(pet_name, count) }}
* Type: {{ attribute(type, count) }}
* Age: {{ attribute(age, count) }}
* Gender: {{ attribute(gender, count) }}

{% set count = count + 1 %}

{% endfor %}

Comments

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.