Re: Help with variable variables not being set for a multi-dimensional array
| From: | David Harkness | Date: | Wed, 10 Nov 2010 19:36:39 +0000 |
| Subject: | Re: Help with variable variables not being set for a multi-dimensional array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-309431@lists.php.net to get a copy of this message | ||
On Tue, Nov 9, 2010 at 6:55 PM, Daevid Vincent <daevid@daevid.com> wrote:
> I've used variable variables before but for some reason I can't figure this
> snippet out. Why doesn't $ini_file get set (or appended to).
>
AFAIK variable variables can only reference actual variables--not array
subscripts or other non-variable syntax elements such as "->" or "::".
eval() can do this because it parses the code as PHP. Variable variables
take the *variable name* contained in the variable and look it up in the
current scope. This is a variable name:
ini_array
This is not:
ini_array['agis_core']['adapter']
I think you can use references here to do what you need. Warning: I only
tested the basics of this in the interpreter without running this exact
code.
public function explode_ini()
{
$ini_array = array();
foreach($this->ini_array as $heading => $key_vals)
{
foreach ($key_vals as $k => $v)
{
$path = &$ini_array[$heading];
$subsection = explode('.', $k);
foreach ($subsection as $ss)
$path = &$path[$ss];
$path = $v;
unset($path);
}
}
$this->ini_array = $ini_array;
}
David