简体   繁体   English

使用 implode()、join() 或 json_encode() 将 PHP 数组传递给 Javascript 数组

[英]Passing PHP array to Javascript array using implode(), join() or json_encode()

I am working on a Wordpress plugin and I need to pass PHP array to Javascript array.我正在开发一个 Wordpress 插件,我需要将 PHP 数组传递给 Javascript 数组。 I have tried using join(), implode() and even Json_encode.我尝试过使用 join()、implode() 甚至 Json_encode。 But, the wordpress is not displaying any value.但是,wordpress 没有显示任何值。

When using join(), I used the code:在使用 join() 时,我使用了代码:

<?php
$php1 = array(1,2,3);

?>
<script language='Javascript'>
var lat = ["<?php echo join("\", \"", $php1); ?>"];
document.write(lat[1]);
</script>

If used on localhost(without wordpress), the above code provides a valid output. But, somehow, its not working on Wordpress. The "apache error log" show this message:如果在本地主机(没有 wordpress)上使用,上面的代码提供了一个有效的 output。但是,不知何故,它在 Wordpress 上不起作用。“apache 错误日志”显示此消息:

PHP Warning: join() [function.join]: Invalid arguments passed in \wp-content\plugins\Animation\animation.php on line 129, referer: http://localhost/Website/wp-admin/options-general.php?page=js PHP 警告:join() [function.join]:无效的 arguments 在第 129 行的 \wp-content\plugins\Animation\animation.php 中传递,referer: http://localhost/Website/wp-admin/options-general。 php?页面=js

Same is the case with implode(). implode() 也是如此。 Server error log shows same above warning for implode().服务器错误日志显示与 implode() 相同的上述警告。

Then I tried for json_encode using the code below:然后我尝试使用下面的代码进行 json_encode:

var lat = <?php echo json_encode($php1); ?>;

But the no value is returned.但是没有返回任何值。

Edit: Code I used for JSON:编辑:我用于 JSON 的代码:

<?php
/*
Plugin Name: PHPToJavascript
*/
$arr = array(1,2,3,4,5,6,7,8,9);            //array to pass

add_action('admin_menu','admin_jav');

function admin_jav(){
add_submenu_page('options-general.php','Javarray','Javarray','manage_options','javarray',jav_handler);
}

function jav_handler(){
echo 'Into handler';

?>
<SCRIPT LANGUAGE = 'Javascript'><!--
var sm=<?php echo json_encode($arr); ?>;      //using Json  

document.write(sm[1]);                       //doesnt display any output!!!

</SCRIPT>
<?php
}
?>

Please guide me through this.请指导我完成这个。 I appreciate any help.我感谢任何帮助。 It would be great if you help me in passing this PHP array to javascript array.如果您能帮我将这个 PHP 数组传递给 javascript 数组,那就太好了。

Change:改变:

var lat = ["<?php echo join("\", \"", $php1); ?>"];
document.write(lat[1]);

to:到:

var lat = ["<?php echo join('", "', $php1); ?>"];
document.write(lat[0]);

The $arr variable is out of scope. If you want to use the global $arr variable, you need to modify your jav_handler() function to bring the variable into local scope: $arr变量在scope之外,如果要使用全局$arr变量,需要修改你的jav_handler() function,将变量带入本地scope:

function jav_handler(){
    global $arr;
    // ...

However, it's good practice to always avoid global variables when you can, so the preferred way of doing this is to change the function to take the array as an argument and pass it explicitly when calling the function:但是,最好始终尽可能避免使用全局变量,因此首选方法是将 function 更改为将数组作为参数,并在调用 function 时显式传递它:

function jav_handler($arr){
    // ...
}

jav_handler($arr);

Instead of:代替:

var lat = ["<?php echo join("\", \"", $php1); ?>"];

I would try:我会尝试:

var lat = "<?php echo json_encode($php1); ?>";

Additionally, you may want to use a Browser that offers proper JS debugging.此外,您可能希望使用提供适当 JS 调试的浏览器。 In chrome, you can use console.log(lat);在 chrome 中,你可以使用console.log(lat); to see exactly what lat holds看看 lat 到底有什么

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

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