Wednesday, 17 April 2019

Passing php array into javascript variable


<?php
// php array
$fruits = array('apple', 'orange', 'mango', 'banana', 'strawberry');
?>

<script type="text/javascript">
// use php implode function to build string for JavaScript array literal
var fruits = <?php echo '["' . implode('", "', $fruits) . '"]' ?>;
// ["apple", "orange", "mango", "banana", "strawberry"]
</script>
source : 

https://www.dyn-web.com/tutorials/php-js/array.php

No comments:

Post a Comment