note 42156 added to language.types.array

From: Date: Wed, 05 May 2004 15:44:25 +0000
Subject: note 42156 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-69248@lists.php.net to get a copy of this message
With arrays sometimes you need variable depth arrays. For instance: create just in PHP an array from <? $arr = array("a","b","c"); ?> That looks like <? Array([a] => Array([b] => Array([c] => Array()))) ?> I tried various ways like ${$parts}='test'; etc in the below and finally found that you can just create a string and eval it. It can be done like the following: <? $parts = "$"."test"; while(list($key,$val)=each($arr)){ $parts .= "['$val']"; } $parts .= "='test';"; eval($parts); print_r($parts); ?> Comes in handy (and saves a LOT of coding) when doing things with xml and rdf where you do not know the actual depth the array will need to be. ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42156 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42156&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42156&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69248) next »