2

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!

Andrew
  • 21
  • 2
  • I'm still not entirely clear on what you're trying to do. Perhaps you could include a redacted version of your form, and the PHP code as far as you've got it? – ghoti Feb 23 '12 at 12:35

3 Answers3

2

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]};
Vitamin
  • 1,526
  • 1
  • 13
  • 27
  • Thanks for the reply - this code is very useful, but not exactly what I need. Currently, I have access to all of the variables by name. e.g. field_anp_1 can be directly printed now. However, I am trying to print them from within a for loop. What I've actually done is created an array with the suffixes that I will need to print. My goal is to loop through that array and print the $field_anp_$array[$x] variable. – Andrew Feb 23 '12 at 12:27
  • thanks for the updated code. I was able to figure it out and your answer was very useful. I posted an answer for other folks with the same problem using drupal - perhaps it will show you exactly what I was after. – Andrew Feb 23 '12 at 12:53
  • Just accepted it. The only change I would recommend is for the drupal developers. To access $field_anp_123[0]['value'] using variable variables, they need to do the following: `$var = 'field_anp_' . $array[$x]; print ${$var}[0]['value'];` – Andrew Feb 23 '12 at 15:57
2

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});
}
ghoti
  • 45,319
  • 8
  • 65
  • 104
  • This will set a hard coded limit of retrieved variables, when the number of variables may, to my understanding, vary. – Vitamin Feb 22 '12 at 20:10
  • This short script was intended to demonstrate how to use variables as names for variables. The exact application with Drupal would require a bit more detail than is provided in the question. If the list of variables is sequential, then walking through them until `isset()` is false would be sufficient to hit all of them. – ghoti Feb 22 '12 at 20:21
  • This is along the lines of what I was thinking. However, when I try to implement it, it does not work. $field_anp_X is structured like this: $field_anp_1[0]['value']. For some reason, it seems that having an array is causing issues. – Andrew Feb 22 '12 at 20:37
  • Also - the variables aren't actually sequential. But, what I've done instead is create an array with the ending numbers. So instead of $i , I'm using $courses[$i] within the for loop. – Andrew Feb 22 '12 at 20:43
  • Thanks for your code, ghoti - it helped me understand variable variables a lot better. I was able to come up with the code in my answer because of your post. – Andrew Feb 23 '12 at 12:52
0

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!

Andrew
  • 21
  • 2