you can see my goal here,
i have a php array in my code like
<?php $myvalues = array(13,45,23,54,767,234,543,245); ?>
i want to store all the values in jquery array in my script
<script>
$(document).ready(function(){
var jqueryarray = $myvalues; // here i want to store $myvalues array values in jqueryarray
for (var i = 0; i < jqueryarray.length; i++) {
//my stuf
};
});
</script>
How can i do this one ?
Any ideas ?
You can easily use PHP array in javascript you only need to convert PHP array into JSON format Using json_encode() function. PHP array can be converted to JavScript array and accessible in JavaScript. Whatever the array type is, a single or multidimensional or indexed or associative array.
You can use json_encode
,
var jqueryarray = <?php echo json_encode($myvalues); ?>;
Try this:
var jqueryarray = JSON.parse('<?php echo json_encode($myvalues); ?>');
Pretty simple and you were almost there. Use the below code and of course in php file
<?php $myvalues = array(13,45,23,54,767,234,543,245); ?>
<script>
$(document).ready(function() {
var jqueryarray = <?php echo json_encode($myvalues ); ?>;
for (var i = 0; i < jqueryarray.length; i++) {
console.log(jqueryarray[i]);
}
;
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With