JAVASCRIPT Code Function Converts PHP Array to JS Array

JS Code to Convert PHP Array to Javascript [php echo json_encode();] is only PART of The Answer to Convert PHP Array to Javascript Array

Home Short:

To convert a php array to Javascript array requires the use of 3 json/javascript functions.

  1. json_encode encodes the php array to JSON string format.
  2. encodeURI escapes any characters in the string which may raise an error and prevent the converting of php array to javascript array. For example encodeURI escapes carriage returns, tabs, new line characters, etc.
  3. JSON.parse function to convert the string to a Javascript object which is what an array is.

The php array does not have to be an associative array for this to work.

S
H
A
R
E