drupal 中的可变变量
我正在主题化 drupal 内容类型,并且我有一组类似命名的变量。例如,field_anp_1、field_anp_2、...、field_anp_10。我想从 for 循环中动态打印它们。通常,人们会通过执行以下操作来单独打印出值: print $field_anp_1[0]['value'];
就我而言,我无法执行此操作,因为最后一个数字发生了变化。那么,在 for 循环中,如何打印这些字段呢?我尝试了可变变量,但我似乎不太明白那里到底发生了什么 - 而且我认为它不喜欢数组中的事实。任何帮助将不胜感激!
I am theming a drupal content type, and I have a set of similarly named variables. e.g. field_anp_1, field_anp_2,..., field_anp_10. I want to dynamically print them out from within a for loop. Normally, one would print the values out individually by doing something like:print $field_anp_1[0]['value'];
in my case, I can't do this because the last number changes. So, within a for loop, how would one print out these fields? I tried variable variables, but I don't seem to understand exactly what is going on there - and I don't think it likes the fact that this in an array. Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绝对不是数组。但是您可以使用
{..}
来使用变量作为变量的名称。或者,如果您确定存在的变量将是连续的。你可以在失败时停止(根据下面的评论):
Definitely not an array. But you can use a variable as the name of a variable with
{..}
Alternately, if you are sure that the variables that do exist will be sequential. you can stop on failure (per comments below):
我看不出有什么理由像这样生成无数的变量。但这就是收集它们的方法:
现在您可以将值作为关联数组存储在
$vars
中。您可以直接打印它们,而不是将这些值添加到$vars
中。更新回应您的评论
I can see no reason for having an untold number of variables generated like that. But this is how you could collect them:
Now you would have your values as an associative array in
$vars
. Instead of adding the values to$vars
, you could print them directly.Update In response to your comment
好吧,我明白了。我只是需要更具体地了解 PHP。要在 for 循环中调用诸如 $field_anp_0[0]['value'] 之类的变量(其中 0 不断增加),只需执行以下操作:
这将允许我列出我需要按照打印顺序打印的字段。然后,我可以使用 for 循环来打印主题表。
谢谢您的帮助!
Ok, I figured it out. I simply needed to be more specific with PHP. To call a variable such as:
$field_anp_0[0]['value']
from within a for loop, where 0 is increasing, one simply needs to do the following:This will allow me to list the fields that I will need to have printed out in the order I need to have them printed out. Then, I can use a for loop to print out a themed table for instance.
Thank you for the help!