树枝:按键"0"不存在,因为数组为空,但转储显示数组 [英] Twig: Key "0" does not exist as the array is empty but dump show array
问题描述
我在Twig尝试显示数组时遇到问题.
I have a problem in Twig trying to show an array.
当我转储变量 {{dump(perfilServidor)}}
时,我得到:
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 ▶}
}
]
但是当我尝试转储数组 {{dump(perfilServidor [0])}}
的一个键时,它将引发异常:
But when I try to dump one key of the array {{ dump(perfilServidor[0]) }}
, it throws an Exception:
键"0"不存在,因为数组为空
Key "0" does not exist as the array is empty
如果我尝试访问另一个键, {{dump(perfilServidor [5])}}
,它表示数组具有键0,1,2:
And if I try to access another key, {{ dump(perfilServidor[5]) }}
, it says that the array have the keys 0,1,2:
具有键"0、1、2"的数组的键"5"不存在
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?
推荐答案
即使我对您正在构建的应用程序一无所知,我也可以告诉您使用硬编码数组键是一种不好的做法,尤其是在模板内部.
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.
这篇关于树枝:按键"0"不存在,因为数组为空,但转储显示数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!