PHP - Convert PHP Array To JavaScript Array

In this tutorial we will create a Convert PHP Array To Javascript Array using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-2"></div>
  14.         <div class="col-md-8 well">
  15.                 <h3 class="text-primary">PHP - Convert PHP Array To Javascript Array</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                
  18.                         <div class="col-md-6">
  19.                                 <h4>PHP Array</h4>
  20.                                 <?php
  21.                                         $county = array(
  22.                                                 "Philippines",
  23.                                                 "USA",
  24.                                                 "France",
  25.                                                 "Japan",
  26.                                                 "China",
  27.                                                 "Germany",
  28.                                                 "Africa"
  29.                                         );
  30.                                         echo "<pre>";
  31.                                         print_r($county);
  32.                                         echo "</pre>";
  33.                                 ?>
  34.                         </div>
  35.                         <div class="col-md-6">
  36.                                 <h4>Javascript Array</h4>
  37.                                 <pre id="result"></pre>
  38.                         </div>
  39.                         <br style="clear:both;"/>
  40.                         <center><button onclick="arrayToJs()" class="btn btn-primary">Convert</button></center>
  41.         </div>
  42. </body>
  43. <?php include 'convert.php'?>
  44. </html>

Creating Convert Script

This is the main function of the application. This code will convert the php array to javascript array when the button is clicked. To do that copy and write these inside the text editor, then save it as convert.php
  1. <script type="text/javascript">
  2. function arrayToJs(){
  3.         var array= <?php echo json_encode($county); ?>;
  4.         var target = document.getElementById('result');
  5.         var country = [];
  6.        
  7.         for(var i=0;i<array.length;i++){
  8.                 country.push(array[i]);
  9.         }
  10.        
  11.         target.innerHTML = JSON.stringify(country);
  12. }
  13.  
  14. </script>
There you have it we successfully created Convert PHP Array To Javascript Array using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment