How to assign PHP array values to JavaScript array?
Using below PHP code I am storing data in PHP array:
<?php
$str_query="SELECT title,description FROM tablename ORDER BY title";
$rs_sch=GetRecordset($str_query);
$int_count=0;
$schd_arr = array();
while(!$rs_sch->EOF())
{
$schd_arr[$int_count] = $rs_sch->Fields("title").": ".$rs_sch->Fields("description");
$rs_sch->MoveNext();
$int_count++;
}
?>
Using below JavaScript code I am trying to store PHP array data into JavaScript array
What and how to write variable at below 2 mentioned locations so my code can work?
<script type="text/javascript" language="javascript">
var pausecontent=new Array()
for (sch_cnt=0; sch_cnt<*Here I want to assign value of $int_count php variable*; sch_cnt++)
{
pausecontent[sch_cnt]=<?php *Here I want to assign php array and counter values (something like this - $schd_arr[sch_cnt];)* ?>;
}
</script>
Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation(JSON). Method 1: Using json_encode() function: The json_encode() function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.
PHP array can be used in JavaScript, whatever the array is a single or multidimensional or indexed or associative array. You can convert PHP array to JavaScript array easily with a single line of code. Using json_encode() function, PHP array can be converted to JavScript array and accessible in JavaScript.
To append one array to another, use the push() method on the first array, passing it the values of the second array. The push method is used to add one or more elements to the end of an array. The method changes the contents of the original array. Copied!
Sometimes you need to append one or more new values at the end of an array. In this situation the push() method is what you need. This method accepts an unlimited number of arguments, and you can add as many elements as you want at the end of the array.
PHP array can be used in JavaScript, whatever the array is a single or multidimensional or indexed or associative array. You can convert PHP array to JavaScript array easily with a single line of code. Using json_encode () function, PHP array can be converted to JavScript array and accessible in JavaScript.
Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation(JSON). Method 1: Using json_encode() function: The json_encode() function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.
Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation (JSON). Method 1: Using json_encode () function: The json_encode () function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays. Hey geek!
Method 1: Using json_encode () function: The json_encode () function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays. Method 2: Using PHP implode () function: The implode () is used to join the elements of an array.
You can directly use the json_encode
function. It is easy to use and produces valid javascript so is very stable:
<script type="text/javascript">
var pausecontent = <?php echo json_encode($php_array); ?>;
</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