将关联数组从 php 传递给 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7217054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:20:18  来源:igfitidea点击:

pass associative array from php to javascript

phpjavascriptarraysjsonassociative

提问by Abhimanyu1310

Possible Duplicate:
How to pass an array of strings from PHP to Javascript using $.ajax()?

可能的重复:
如何使用 $.ajax() 将字符串数组从 PHP 传递到 Javascript?

I want to pass a associative array from php code to javascript code. Please help me. how do I do this ? Is JSON helpful in this matter? If yes then please provide a simple code for help. Thank you.

我想将关联数组从 php 代码传递给 javascript 代码。请帮我。我该怎么做呢 ?JSON 在这方面有帮助吗?如果是,那么请提供一个简单的代码以获得帮助。谢谢你。

From comment below:
HTML + PHP code

来自下面的评论:
HTML + PHP 代码

<td> 
    <input type="text" style="width:70;" name="<?php echo $quantity;?>" id="<?php echo $quantity;?>" onkeyup="check_valid_range('<?php echo $itemName;?>','<?php echo $quantity;?>',<?php echo json_encode($product_inventory);?>);">
</td> 
<script type="text/javascript"> 
    function check_valid_range(product_field, quantity_field, inventory){ 
        var product = document.getElementById(product_field).value; 
        var quantity = document.getElementById(quantity_field).value; 
        var v = inventory[product]; 
        alert(v); 
    } 
</script>

回答by afuzzyllama

JSON is perfect:

JSON 是完美的:

<?php

$mySweetJSONString = json_encode($myAssocPHPArray);

?>
<script>
    var iWantThePHPArrayHere = <?php echo $mySweetJSONString; ?>;
</script>

User pstbrought up these concerns:

用户pst提出了这些问题:

  • "array("</script>") -- darn, just broke this "perfect" approach."
    It seems to work because (</script>=> <\/script>):
    jsFiddle

  • "What about ]]> which could occur in XHTML?"
    The string is able to be transferred.
    jsFiddle

  • "array("</script>") -- darn, just broke this "perfect" approach."
    它似乎有效,因为 ( </script>=> <\/script>):
    jsFiddle

  • "What about ]]> which could occur in XHTML?"
    字符串是可以传输的。
    js小提琴



Update:

更新:

In regards to debugging the problem with the JS:
Are there any errors in the console? One of your document.getElementById(...)might be returning a null. Therefore the member valuedoesn't exist.

关于用JS调试问题:
控制台有没有错误?其中一个document.getElementById(...)可能返回空值。因此该成员value不存在。

回答by Arnaud Le Blanc

I always believed JSON were invented for this:

我一直相信 JSON 是为此而发明的:

<script>
    var myArray = <?=json_encode($myArray)?>;
</script>

This will render like this:

这将呈现如下:

<script>
    var myArray = {"key":"value","key2":"value2",...};
</script>

Safeness

安全性

This is perfectly safe for javascript, as JSON is a subset of the javascript syntax. This means that any JSON string can be treated as Javascript.

这对 javascript 来说是完全安全的,因为 JSON 是 javascript 语法的一个子集。这意味着任何 JSON 字符串都可以被视为 Javascript。

PHP's json_encode()is perfectly safe when embedding JSON in <script>tags too, because PHP escapes the /character:

json_encode()<script>标签中嵌入 JSON 时,PHP也是完全安全的,因为 PHP 会转义/字符:

json_encode('</script>');
=> "<\/script>"

So it's not possible to write </script>, which is the only way to escape a <script>tag. (Everything in a <script>tag is part of the script, it's not parsed as HTML.) JSON allows to escape the /for this purpose.

所以不可能写</script>,这是转义<script>标签的唯一方法。(<script>标签中的所有内容都是脚本的一部分,不会被解析为 HTML。)为此,JSON允许转义/

So no JSON string can escape a <script>tag.

所以没有 JSON 字符串可以转义<script>标签。