0

I have two arrays: teachers_number_array which contains the id of teachs and the another array 'teachers' witch contains the data of each teacher.

What I can't do is to show the name of a specific teacher based on its id:

{% for teachers_number in teachers_number_array %}
    {% if teachers_number in teachers|keys %}
        {{ teachers.teachers_number.name }} 
    {% endif %}
{% endfor %}
2
  • {{ teachers[teachers_number].name }} or attribute(teachers, teacher_number).name) Commented Oct 27, 2017 at 5:37
  • Did you tried ? Commented Nov 4, 2017 at 6:27

1 Answer 1

1

You can do something like this. Note that you can get the key of the array directly in the Twig loop (for id, teacher in teachers):

PHP variables:

$teachers_to_display = [2, 3];
$teachers =  [
    1 => 'Fabien',
    2 => 'COil',
    3 => 'Tokeeen',
    'do not display' => 'Nooooo',
];

Twig:

{% for id, teacher in teachers %}
    {% if id in teachers_to_display %}
        {{ teacher }}
    {% endif %}
{% endfor %}

Will output:

  • COil
  • Tokeeen

PS: If you have several properties just use teacher.name like you did before:

$teachers =  [
    1 => [
        'name' => 'Pot',
        'firstname' => 'Fabien',
...
Sign up to request clarification or add additional context in comments.

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.