note 29542 added to ref.array

From: Date: Mon, 17 Feb 2003 04:05:48 +0000
Subject: note 29542 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-44078@lists.php.net to get a copy of this message
Instant JavaScript array from PHP array (any combination of associative / numeric sub-arrays): function arrayToJS ($theArray, $jsArrayName) { $jsArrayString = $jsArrayName . " = new Array ();\n"; foreach ($theArray as $key => $val) { if (is_numeric ($key)) { $jsElement = $key; } else { $jsElement = '\'' . $key . '\''; } if (is_array ($val)) { $jsArrayString .= arrayToJS ($val, $jsArrayName . '[' . $jsElement . ']'); } else { if (is_numeric ($val)) { $jsValue = $val; } else { $jsValue = '\'' . $val . '\''; } $jsArrayString .= $jsArrayName . '[' . $jsElement . '] = ' . $jsValue . ";\n"; } } return $jsArrayString; } //----------------usage------------------// echo '<script language="JavaScript" type="text/javascript">'; echo arrayToJS ($myArray, 'myJSArray'); echo '</script>'; // for some reason the PHP site keeps scrambling my indentation .. :( -- http://www.php.net/manual/en/ref.array.php http://master.php.net/manage/user-notes.php?action=edit+29542 http://master.php.net/manage/user-notes.php?action=delete+29542 http://master.php.net/manage/user-notes.php?action=reject+29542

« previous php.notes (#44078) next »