note 48963 added to language.types.array

From: Date: Thu, 13 Jan 2005 10:59:19 +0000
Subject: note 48963 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-83190@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48963 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48963&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48963&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48963&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48963&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48963&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48963&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48963&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83190) next »