1
votes

I have a problem in Twig trying to show an array.

When I dump the variable {{ dump(perfilServidor) }}, I get:

array:3 [▼
  0 => UypPerfilServidor {#6122 ▼
    -id: 1
    -idPerfil: 8
    -uypPerfil: UypPerfil {#363 ▶}
    -uypServidor: UypServidor {#6118 ▶}
  }
  1 => UypPerfilServidor {#6117 ▼
    -id: 2
    -idPerfil: 8
    -uypPerfil: UypPerfil {#363}
    -uypServidor: UypServidor {#6116 ▶}
  }
  2 => UypPerfilServidor {#6114 ▼
    -id: 3
    -idPerfil: 8
    -uypPerfil: UypPerfil {#363}
    -uypServidor: UypServidor {#6115 ▶}
  }
]

But when I try to dump one key of the array {{ dump(perfilServidor[0]) }}, it throws an Exception:

Key "0" does not exist as the array is empty

And if I try to access another key, {{ dump(perfilServidor[5]) }}, it says that the array have the keys 0,1,2:

Key "5" for array with keys "0, 1, 2" does not exist

I want to get one specific value of the array (without looping through the whole array) How do I get the values of the array without looping through it?

1

1 Answers

0
votes

Even if i don't know anything about the application that you are building, i can tell you that it is a bad practice to use hard coded array keys, especially inside a template.

You should not give a whole array to a template if you only need one item. Remember that the template is only the view not your program-logic.