Skip to content Skip to sidebar Skip to footer

PHP array convert to Java Script array

Reading Time: < 1 minute

Sometimes when we using php, we want to convert php array to java script array.php array can be convert to java script easily.Here is a easy and best methods to do this.
PHP provides json_encode() function to convert PHP arrays into Java Script code.

$php_array = array('id','username','email');

This can be convert to java script as follows.


Note:json_encode()

 id [1] => username [2] => email )
*/
$js_array = json_encode($php_array);
print_r($js_array);
/* Apply json_encode()
   ["id","username","email"]
*/
?>