Re: How to use a variable variable with an array [solved]
| From: | Nils Leideck | Date: | Thu, 15 Dec 2011 04:19:05 +0000 |
| Subject: | Re: How to use a variable variable with an array [solved] | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316022@lists.php.net to get a copy of this message | ||
Dear Robert,
On 14.12.2011, at 22:02, Robert Cummings wrote:
> $path = ‘/path/to/data/in/my/arbitrarily/deep/array/';
> $array = $someBigFatArray;
> $focus = &$array;
> foreach( explode( '/', $path ) as $step )
> {
> if ($step === '' || $step === 'CONFIGURATION’)
> {
> continue;
> }
> if( isset( $focus[$step] ) )
> {
> $focus = &$focus[$step];
> }
> else
> {
> unset( $focus );
> $focus = null;
> break;
> }
> }
> echo "<pre>"; echo var_export($focus, TRUE); echo "</pre>";
This solution is excellent! I modified it a little to match for empty $step and then continue, just
in case there is a leading, ending or doubled “/“ and same for CONFIGURATION which will be my
indicator to find the values where we expect a value replacement.
if ($step === '' || $step === 'CONFIGURATION’) {
continue;
}
Great thing!
Thanks a lot to all!
Cheers, Nils
--
http://webint.cryptonode.de / a Fractal project