How to convert PHP array to JavaScript or JSON ?
PHP provides a json_encode() function that converts PHP arrays into JavaScript. Technically, it is in JSON format. JSON stands for JavaScript Object Notation.
Statement: If you have a PHP array and you need to convert it into the JavaScript array so there is a function provided by PHP that will easily convert that PHP array into the JavaScript array. But before using this function you need few things that first make sure you are using PHP version 5.2 or above. Next, use the library function json_encode() to convert the PHP array to JavaScript array.
Syntax:
json_encode( $my_array );
Example 1: This example uses json_encode() function to convert PHP array to JavaScript JSON object.
<?php // Array in php $myArr = array('Geeks', 'GeeksforGeeks@geeks.com'); ?> <!-- Converting PHP array into JavaScript array --> <script> var arr = <?php echo json_encode($myArr); ?>; document.write(arr[1]); </script> <?php ?> |
Output:
GeeksforGeeks@geeks.com
Example 2: Here you will see converting single dimensional PHP array into javaScript array by using json_encode($myArr). Passing the php array and then using json_encode, we convert it into javascript array.
<?php ?> <script type='text/javascript'> <?php $php_array = array('geeks', 'for', 'geeks'); $js_array = json_encode($php_array); echo "var javascript_array = ". $js_array . ";\n"; ?> document.write(javascript_array[0]); </script> <?php ?> |
Output:
geeks
Example 3: Here you will see converting multi-dimensional PHP array into javaScript array by using json_encode($myArr). Passing the php array and then using json_encode, we convert it into javascript array.
<?php ?> <script type='text/javascript'> <?php $php_array = array( array('Geeks', 'for@example.com'), array('for', 'gfg@example.com'), ); $js_array = json_encode($php_array); echo "var javascript_array = ". $js_array . ";\n"; ?> document.write(javascript_array[0][1]); </script> <?php ?> |
Output:
for@example.com
Note:It should be noted that json_encode() function is only available in PHP 5.2 or after versions.
Recommended Posts:
- JavaScript | Convert an array to JSON
- How to convert JSON results into a date using JavaScript ?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to convert JSON data to a html table using JavaScript/jQuery ?
- JSON | modify an array value of a JSON object
- Convert Json String to Java Object Using GSON
- Python | Ways to convert string to json object
- Convert Java Object to Json String using GSON
- Convert Java Object to Json String using Jackson API
- How to convert Set to Array in JavaScript?
- How to Convert Array to Set in JavaScript?
- How to convert Map keys to an array in JavaScript ?
- Convert an Array to an Object in JavaScript
- How to convert arguments object into an array in JavaScript ?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai


