Convert PHP array into Javascript Array


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. $arreglo = array(
  3. 'clave 1' => 'valor 1',
  4. 'clave 2' => 'valor 2',
  5. 'clave x' => 'valor x',
  6. );
  7. ?>
  8.  
  9. <script language="javascript">
  10. var arr = {
  11.  
  12. <?php
  13. foreach($arreglo as $key => $values){
  14. $arr .= "'" . $key . "'" . " : " . "'" . $values . "'" . ",";
  15. }
  16. $arr = substr_replace($arr, '', -1);
  17. echo $arr;
  18. ?>
  19.  
  20. };
  21. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.