将PHP数组保存为javascript数组变量
我使用下面的一对javascript和php文件来获取目录中所有图像文件的名称 Javascript将PHP数组保存为javascript数组变量,javascript,php,arrays,Javascript,Php,Arrays,我使用下面的一对javascript和php文件来获取目录中所有图像文件的名称 Javascript $(document).on("click", "ul#matchListUL li a", function(event){ event.preventDefault(); var matchLst = $(this).attr('href'); var filenames = new Array(); if (matchLst !== null
$(document).on("click", "ul#matchListUL li a", function(event){
event.preventDefault();
var matchLst = $(this).attr('href');
var filenames = new Array();
if (matchLst !== null && matchLst !=="") {
$.ajax({
url : "filenames.php",
dataType : "JSON",
type : "POST",
success:function(data) {
console.log(data);
}
});
}
return false;
})
filename.php
$tardir = "mysite.com/projects/" . $seldir . "/" . $match . "/*.jpg" ;
$files = glob($tardir);
$fileName = array();
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
$fileName = basename($num, ".jpg");
}
echo json_encode($fileName);
$tardir=“mysite.com/projects/”$塞尔迪尔。"/" . $匹配。“/*.jpg”;
$files=glob($tardir);
$fileName=array();
对于($i=0;$i您做错了,您应该这样做:
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
// [] will add the value to the array
$fileName[] = basename($num, ".jpg");
}
$fileName[]=basename($num,.jpg”);
@splash58谢谢。“谢谢。我会接受你的回答”是吗?
foreach($files as $file){
$fileName[] = basename($file, ".jpg");
}