带有drupal的变量变量

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!

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
", ${$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!

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]};

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
", $i, ${$thisvar});
  } else {
    printf("%s: not set
", $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
", $i, ${$thisvar});
}