Created
August 3, 2016 01:26
-
-
Save danielrw7/a59ccea6e0f918cc44bb73afa05177a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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