Skip to content

Instantly share code, notes, and snippets.

@danielrw7
Created August 3, 2016 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielrw7/a59ccea6e0f918cc44bb73afa05177a7 to your computer and use it in GitHub Desktop.
Save danielrw7/a59ccea6e0f918cc44bb73afa05177a7 to your computer and use it in GitHub Desktop.
<?php
function export_variables_to_js($vars = array(), $parent = "window") {
$js_vars = array();
foreach ($vars as $key => $val) {
$key = json_encode($key);
$val = json_encode($val);
if ($parent) {
$js_vars[] = "$parent[$key] = $val";
} else {
$js_vars[] = "var $key = $val";
}
}
if (count($js_vars)) {
return "<script>" . implode("; ", $js_vars) . ";</script>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment