note 55713 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 21:57:53 +0000
Subject: note 55713 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199431@lists.php.net to get a copy of this message
Note Submitter: dieter peeters ---- in response to: Domenic Denicola I reworked your function a bit and thought i just as well could post it. Below is the cleaner version, just cut and paste ;) The third parameter is of little use to the coder, unless javascript declaration of variables changes at some point in the future - who knows. Only minor point is the added parameter which probably gets copied every recursive call with an empty value, though i don't know the exact ways how php handles recursion internally. Most of the time php is pretty smart when optimizing code and an empty string shouldn't take much memory anyway :) <?php function phpArrayToJsArray($name,$array,$prePend='var ') { if (is_array($array)) { // Array recursion $result = $name.' = new Array();'."\n"; foreach ($array as $key => $value) { $result .= phpArrayToJsArray($name.'["'.$key.'"]',$value,''); } } else { // Base case of recursion $result = $name.' = "'.$array.'";'."\n"; } return $prePend.$result; } ?>

« previous php.notes (#199431) next »