Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

How to convert PHP array to JavaScript array?



You can use PHP array in JavaScript. It works for the single as well as the multidimensional array. Use the json_encode() method to achieve this.

Let’s say Our PHP array is −

$myArr = array('Amit', '[email protected]');

Converting PHP array into JavaScript.

<script>
var arr = <?php echo json_encode($myArr); ?>;
</script>

Now, let’s finally learn how to access it to output the email −

document.write(arr[1]);
Updated on: 2020-06-18T12:30:17+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements