php和javascript之间的var transfert [英] var transfert between php and javascript
本文介绍了php和javascript之间的var transfert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢在php中获取目录列表
I like to get a dir listing in php
glob("*.jpg");
或
$dir = '.'; //requested directory to read
$notthat = array('.', '..'); //what not to include
$listedfiles = array_diff(scandir($dir), $notthat); // removed what not to include
所以我想把那个数组发送到这样的javascript(幻灯片) = $ listedfiles)
so i like to send that array to a javascript like that (slides = $listedfiles)
function startSlideshow(slides) { .. do something..}
最好的方法是什么?
推荐答案
json_encode 是您的朋友。不需要循环。它将返回一个纯json对象字符串,然后您可以使用PHP将其回显到您的js文件中。示例:
json_encode is your friend for this. No looping is necessary. It will return a pure json object string that you can then just echo into your js file using PHP. Example:
var slides = <?php echo json_encode( $filelistarray );?>
function startSlideshow(slides) { .. do something..}
这篇关于php和javascript之间的var transfert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文