开发者

how to send php array data to javascript variable

Here is the php array i am using. I am trying to convert each entry into a string and pass it to the javascript variable, but when i use the following code I only get the first letter of each word in the php array.

<?php 
$terms = array('cat', 'dog', 'bird');
$rand_keys = array_rand($terms);
?>

Javascript variable with php array data

var searchterms开发者_运维百科 = <?php echo json_encode($terms[$rand_keys]); ?>;


use shuffle($terms); then echo json_encode( $terms );


The following code just take a random element from the php array and not the whole array:
var searchterms = <?php echo json_encode($terms[$rand_keys]); ?>;

Use the following code to get all the elemenets:
var searchterms = <?php foreach ($terms as $t){echo json_encode($t);} ?>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜