|
Posted by Michael Fesser on 08/14/07 14:18
..oO(Luca Cioria)
>I know it may seem odd, but is it possible to echo to php? I mean
>neasting php within php?
eval()
But you don't want that, eval is evil. There's always another way.
In most cases a better algorithm will also be more efficient and faster
than eval().
>example
>
><?php
>
>$array_levels="[1][2][3]";
>
>$my_array<?php echo $array_levels; ?>="hello world"
>
>?>
>
>It may seem stupid but would be powerful, wouldn't it?
For a quick shot or a test - maybe, but not for production code.
>How can I accomplish what I just did differently?
Split your $array_levels (a different syntax like "1/2/3" might help),
then walk down the $my_array step by step in a loop or recursively.
I use the following method in my own framework to fetch a value from the
application's registry (configuration data and such things):
public final function getValue($key) {
$current = &$this->registry;
$subkey = strtok($key, '/');
while ($subkey) {
if (!isset($current[$subkey])) {
return NULL;
}
$current = &$current[$subkey];
$subkey = strtok('/');
}
return $current;
}
$this->registry is the nested array, $key is the path to the element I
want to fetch from the tree (in "1/2/3" syntax). The method just
traverses the tree by splitting the path with strtok() and loops through
the tree until the result is found.
Micha
Navigation:
[Reply to this message]
|