php和javascript之间的var transfert

men*_*mam 1 javascript php

我喜欢在php中获取目录

glob("*.jpg");
Run Code Online (Sandbox Code Playgroud)

要么

$dir    = '.'; //requested directory to read
$notthat = array('.', '..');  //what not to include
$listedfiles = array_diff(scandir($dir), $notthat); // removed what not to include
Run Code Online (Sandbox Code Playgroud)

所以我想把那个数组发送到这样的javascript(slides = $ listedfiles)

function startSlideshow(slides) { .. do something..}
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?

Kev*_*eno 6

json_encode是你的朋友.不需要循环.它将返回一个纯json对象字符串,然后您可以使用PHP将其回显到您的js文件中.例:

var slides = <?php echo json_encode( $filelistarray );?>
function startSlideshow(slides) { .. do something..}
Run Code Online (Sandbox Code Playgroud)