drupal 中的可变变量

发布于 2025-01-08 08:10:55 字数 285 浏览 0 评论 0原文

我正在主题化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半岛未凉 2025-01-15 08:10:55

绝对不是数组。但是您可以使用 {..} 来使用变量作为变量的名称。

ghoti@pc:~ $ cat invar.php
#!/usr/local/bin/php
<?php

$field_anp_3="three";
$field_anp_2="two";

for ($i=1; $i<5; $i++) {
  $thisvar="field_anp_" . $i;
  if (isset(${$thisvar})) {
    printf("%s: %s\n", $i, ${$thisvar});
  } else {
    printf("%s: not set\n", $i);
  }
}

ghoti@pc:~ $ ./invar.php
1: not set
2: two
3: three
4: not set

或者,如果您确定存在的变量将是连续的。你可以在失败时停止(根据下面的评论):

#!/usr/local/bin/php
<?php

$field_anp_1="one";
$field_anp_2="two";
$field_anp_3="three";

for ($i=1; $i<5; $i++) {
  $thisvar="field_anp_" . $i;
  if (!isset(${$thisvar})) {
    break;
  }
  printf("%s: %s\n", $i, ${$thisvar});
}

Definitely not an array. But you can use a variable as the name of a variable with {..}

ghoti@pc:~ $ cat invar.php
#!/usr/local/bin/php
<?php

$field_anp_3="three";
$field_anp_2="two";

for ($i=1; $i<5; $i++) {
  $thisvar="field_anp_" . $i;
  if (isset(${$thisvar})) {
    printf("%s: %s\n", $i, ${$thisvar});
  } else {
    printf("%s: not set\n", $i);
  }
}

ghoti@pc:~ $ ./invar.php
1: not set
2: two
3: three
4: not set

Alternately, if you are sure that the variables that do exist will be sequential. you can stop on failure (per comments below):

#!/usr/local/bin/php
<?php

$field_anp_1="one";
$field_anp_2="two";
$field_anp_3="three";

for ($i=1; $i<5; $i++) {
  $thisvar="field_anp_" . $i;
  if (!isset(${$thisvar})) {
    break;
  }
  printf("%s: %s\n", $i, ${$thisvar});
}
樱&纷飞 2025-01-15 08:10:55

我看不出有什么理由像这样生成无数的变量。但这就是收集它们的方法:

$vars = array();
foreach(get_defined_vars() as $name => $value) {
    if(strpos($name, 'field_anp_') === 0) {
        $vars[$name] = $value;
    }
}

现在您可以将值作为关联数组存储在 $vars 中。您可以直接打印它们,而不是将这些值添加到 $vars 中。

更新回应您的评论

$array = array('foo' => 'bar');
$x = 'foo';
$field_anp_bar = 'baz';
echo ${'field_anp_' . $array[$x]};

I can see no reason for having an untold number of variables generated like that. But this is how you could collect them:

$vars = array();
foreach(get_defined_vars() as $name => $value) {
    if(strpos($name, 'field_anp_') === 0) {
        $vars[$name] = $value;
    }
}

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

$array = array('foo' => 'bar');
$x = 'foo';
$field_anp_bar = 'baz';
echo ${'field_anp_' . $array[$x]};
顾铮苏瑾 2025-01-15 08:10:55

好吧,我明白了。我只是需要更具体地了解 PHP。要在 for 循环中调用诸如 $field_anp_0[0]['value'] 之类的变量(其中 0 不断增加),只需执行以下操作:

<?php
$numbers = array(123,235,12332,2342);

for($i; $i<count($numbers); $i++){
    $var = "field_anp_".$numbers[$i];
    printf("%s\n", ${$var}[0]['value']);

}
?>

这将允许我列出我需要按照打印顺序打印的字段。然后,我可以使用 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:

<?php
$numbers = array(123,235,12332,2342);

for($i; $i<count($numbers); $i++){
    $var = "field_anp_".$numbers[$i];
    printf("%s\n", ${$var}[0]['value']);

}
?>

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文