简体   繁体   English

将php数组存储在javascript数组中

[英]storing a php array in a javascript array

This is my php code that gets a list of files in a dir and stores it in the array $files : (Note I tried running this in a fiddle here but not sure if I am able with this example) 这是我的php代码,它获取dir中的文件列表并将其存储在数组$files :(注意我尝试在此处运行此函数但不确定我是否能够使用此示例)

<?php

echo "<hr>";
echo "<hr>";


$files = array_slice(scandir('/path/to/dir'), 2); // this gets a list of all files in the dir

//
echo "this is the array: <br>";
print_r($files); // this prints the array 
echo "<br>";
echo "<br>";

echo "this is each element in the array: <br>";
foreach ($files as &$value) {
    print_r($value."<br>"); // this prints each element of the array 
}

echo "<hr>";
echo "<hr>";
?>

Immediately after that is my javascript where I want to store the php array in my javascript variable, but I get this error Uncaught SyntaxError: Unexpected token ILLEGAL in the console. 紧接着是我的javascript,我想在我的javascript变量中存储php数组,但是我得到这个错误Uncaught SyntaxError: Unexpected token ILLEGAL控制台中的Uncaught SyntaxError: Unexpected token ILLEGAL Can anyone correnct the erro in my ways? 任何人都可以用我的方式纠正错误吗?

<script>
// then echo it into the js/html stream
// and assign to a js variable
var jsfiles =[];
jsfiles = "<?php print_r($files);?>";

// then
alert(jsfiles);
console.log("the files are:",jsfiles);

</script>

EDIT2 tried jsfiles = "<?php json_encode($files);?>"; EDIT2尝试过jsfiles = "<?php json_encode($files);?>"; which gives me no error but values in array are not displayed in console.log("the files are:",jsfiles); 这给了我没有错误但是数组中的值没有显示在console.log("the files are:",jsfiles);


EDIT3 - got it working EDIT3 - 让它工作

snippet of my code. 我的代码片段。 I had to delete the 2 lines below, even though I though they were commented out. 我不得不删除下面的两行,即使我已被注释掉了。 Not sure why 不知道为什么

<script>
// then echo it into the js/html stream
// and assign to a js variable
//var jsfiles =[];
//jsfiles = "<?php print_r($files);?>";   ------ had to delete this line
//jsfiles = <?php json_encode($files) ?>; ------ had to delete this line
var jsfiles = <?php echo json_encode($files) ?>;

Always always always use json_encode when outputting PHP to javascript. 在将PHP输出到javascript时,始终始终使用json_encode

var jsfiles = <?php echo json_encode($files) ?>;

Even if this were just a number or simple string, json_encode protects you from surprises and ensures it will be valid javascript object notation. 即使这只是一个数字或简单的字符串, json_encode可以保护您免受意外,并确保它是有效的javascript对象表示法。

You need to make sure that your output is in pure JSON format so that your javascript can easily parse it to use it. 您需要确保输出采用纯JSON格式,以便您的javascript可以轻松解析它以使用它。

You need to convert is to json: as jszobody suggested you can use json_encode(); 你需要转换为json:因为jszobody建议你可以使用json_encode(); to do that, but you are also required to set headers by... 要做到这一点,但你还需要设置标题...

header('Content-type:application/json;');

By this way javascript will easily parse the output to an javascript json object and you will be able to use it easily... 通过这种方式,javascript将轻松地将输出解析为javascript json对象,您将能够轻松地使用它...

One more tip in final production stage, also put.. 在最后的生产阶段还有一个小费,也放了......

error_reporting(0); 

So only pure JSON output goes to javascript 所以只有纯JSON输出转到javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM