note 48963 deleted from language.types.array by betz
| From: | betz@php.net | Date: | Tue, 18 Oct 2005 06:40:42 +0000 |
| Subject: | note 48963 deleted from language.types.array by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-97145@lists.php.net to get a copy of this message | ||
Note Submitter: Gary Smirny <banderon A imyourgod D com>
----
In responce to NOcom.gmail@ 's comment:
--------------------------------------------------------
I've been trying to create a multidimensional array with an arbitrary depth, but I've run
into a snag:
<?php
$array = Array();
$array[1][2][3] = "huzzah!";
$string = "[4][5][6]";
${"array" . $string} = "boo!";
$array{$string} = "hiss!";
?>
...
It seems that no matter what I do, I can't get PHP to treat the string as actual syntax. Has
anyone else had any luck with arbitrary-depth arrays? I'm not sure whether or not to call this
a bug, nor am I sure if it applies to PHP 5... I've only tested it in 4.3.10.
--------------------------------------------------------
What you need to do is use eval() and treat the command as you would a string. The following code
should do what you want:
<?php
$array = Array();
$array[1][2][3] = "huzzah!";
$string = "[4][5][6]";
eval('$' . 'array' . $string . ' = "boo!"; ');
$array{$string} = "hiss!";
print_r($array);
?>
That will output:
Array
(
[1] => Array
(
[2] => Array
(
[3] => huzzah!
)
)
[4] => Array
(
[5] => Array
(
[6] => boo!
)
)
[[4][5][6]] => hiss!
)
Hope that helps anyone else having this problem.