Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to assign php array values to javascript array

Tags:

javascript

php

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>
like image 737
KRA Avatar asked Oct 10 '12 06:10

KRA


People also ask

How send data from array to JavaScript in PHP?

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.

Can we use PHP array in JavaScript?

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.

Can you assign an array to another array 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!

Can you append to an array in JavaScript?

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.

How to convert PHP array to JavaScript array in JavaScript?

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.

How to pass PHP arrays to JavaScript Object Notation?

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.

How to pass PHP arrays to JavaScript using JSON?

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!

How to join elements of an array in PHP?

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.


1 Answers

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>
like image 146
Hkachhia Avatar answered Oct 22 '22 15:10

Hkachhia