note 67849 added to function.eval

From: Date: Wed, 05 Jul 2006 21:19:05 +0000
Subject: note 67849 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-114433@lists.php.net to get a copy of this message
I was trying to build a multidimensional array to an unknown dimension (within a loop or "while") and found that eval is, as far as I can tell, the only simple way to solve the problem. <?php $arr = array(2, array("v", "q", 5, array(5, 8, "g"), "x")); $i=3; $key1 = "[1]"; $key2 = "[".$i."]"; // E.g., could build this conditionally within a loop $key3 = "[2]"; $keys = $key1.$key2.$key3; // Can add as many keys as needed (could be done instead via a loop with repeated calls to .= ) print $arr{$keys}; // This does not work print $arr[$keys]; // This also does not work // However... eval("\$value = \$arr{$keys};"); print $value; // Correctly prints "g" ?> ---- Server IP: 64.71.164.2 Probable Submitter: 202.105.159.2 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- http://master.php.net/note/edit/67849 Del: integrated -- http://master.php.net/note/delete/67849/integrated Del: useless -- http://master.php.net/note/delete/67849/useless Del: bad code -- http://master.php.net/note/delete/67849/bad+code Del: spam -- http://master.php.net/note/delete/67849/spam Del: non-english -- http://master.php.net/note/delete/67849/non-english Del: in docs -- http://master.php.net/note/delete/67849/in+docs Del: other reasons-- http://master.php.net/note/delete/67849 Reject -- http://master.php.net/note/reject/67849 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114433) next »