Fri, 27 Apr 2018
Home | About us | Web Design | Web Hosting | Solutions | IT Events | Domain Name | Customers | Contact us  
     Technology Information
  Upgrade Windows 10 failed. Re-reserve windows 10
  Pass a PHP Array to Javascript as JSON
  Released new Cloud Server
  Build up new webmail system
  Setup Remote Desktop Server for client
     About us
     Contact us
     Privacy policy
   Technology Information -> Pass a PHP Array to Javascript as JSON

Pass a PHP Array to Javascript as JSON using AJAX and json_encode
You will mostly likely (or already have) reached a point where you have a PHP array and you need to be able to access that array from inside a Javascript function. You may wonder if it is possible to pass an array from PHP to Javascript. Fortunately I have created a tutorial on how this can be achieved. Below I will show you how to Pass a PHP Array to Javascript as JSON using AJAX. There are two parts to accomplish this task.


/* set out document type to text/javascript instead of text/html */
header("Content-type: text/javascript");

/* our multidimentional php array to pass back to javascript via ajax */
$arr = array(
                "first_name" => "Darian",
                "last_name" => "Brown",
                "age" => "28",
                "email" => ""
                "first_name" => "John",
                "last_name" => "Doe",
                "age" => "47",
                "email" => ""

/* encode the array as json. this will output [{"first_name":"Darian","last_name":"Brown","age":"28","email":""},{"first_name":"John","last_name":"Doe","age":"47","email":""}] */
echo json_encode($arr);


About us | Contact us | PrivacyDomain Check | IP Location | Speed Test | QR Create 
Tel: 61 2 9797 2898
Open 6 Days: 10:30am ~ 6:00pm
Wednesday Close day
Shop 2C / 17-20 The Esplanade,
Ashfield NSW 2131 Australia
Copyright(c) 1997 ~ 2015,
Kensoft Technology Pty Ltd., All rights reserved