将 PHP 数组传递给 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4885737/
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
Pass a PHP array to a JavaScript function
提问by Chirayu
I am trying to get a PHP array variable into a JavaScript variable.
我正在尝试将 PHP 数组变量转换为 JavaScript 变量。
This is my code:
这是我的代码:
<html>
<head>
<script type="text/javascript">
function drawChart(row,day,week,month,date)
{
// Some code...
}
</script>
</head>
<body>
<?php
for($counter = 0; $counter<count($au); $counter++)
{
switch($au[$counter]->id)
{
case pageID.'/insights/page_active_users/day':
$day[] = $au[$counter]->value;
break;
case pageID.'/insights/page_active_users/week':
$week[] = $au[$counter]->value;
break;
case pageID.'/insights/page_active_users/month':
$month[] = $au[$counter]->value;
break;
}
}
?>
<script>
drawChart(600/50, '<?php echo $day; ?>', '<?php echo $week; ?>', '<?php echo $month; ?>', '<?php echo createDatesArray(cal_days_in_month(CAL_GREGORIAN, date('m',strtotime('-1 day')), date('Y',strtotime('-1 day')))); ?>');
</script>
</body>
</html>
I can't get value of the PHP array.
我无法获得 PHP 数组的值。
How do I fix this problem?
我该如何解决这个问题?
采纳答案by Chirayu
In the following example you have an PHP array, then firstly create a JavaScript array by a PHP array:
在下面的例子中,你有一个 PHP 数组,然后首先通过一个 PHP 数组创建一个 JavaScript 数组:
<script type="javascript">
day = new Array(<?php echo implode(',', $day); ?>);
week = new Array(<?php echo implode(',',$week); ?>);
month = new Array(<?php echo implode(',',$month); ?>);
<!-- Then pass it to the JavaScript function: -->
drawChart(<?php echo count($day); ?>, day, week, month);
</script>
回答by UltraInstinct
Use JSON.
使用 JSON。
In the following example $php_variable
can be any PHP variable.
在下面的例子中$php_variable
可以是任何 PHP 变量。
<script type="text/javascript">
var obj = <?php echo json_encode($php_variable); ?>;
</script>
In your code, you could use like the following:
在您的代码中,您可以使用以下内容:
drawChart(600/50, <?php echo json_encode($day); ?>, ...)
In cases where you need to parse out an object from JSON-string (like in an AJAX request), the safe way is to use JSON.parse(..)
like the below:
如果您需要从 JSON 字符串中解析出一个对象(如在 AJAX 请求中),安全的方法是使用JSON.parse(..)
如下:
var s = "<JSON-String>";
var obj = JSON.parse(s);
回答by Tarun Gupta
You can pass PHP arrays to JavaScript using json_encode
PHP function.
您可以使用json_encode
PHP 函数将 PHP 数组传递给 JavaScript 。
<?php
$phpArray = array(
0 => "Mon",
1 => "Tue",
2 => "Wed",
3 => "Thu",
4 => "Fri",
5 => "Sat",
6 => "Sun",
)
?>
<script type="text/javascript">
var jArray = <?php echo json_encode($phpArray); ?>;
for(var i=0; i<jArray.length; i++){
alert(jArray[i]);
}
</script>
回答by user3597213
Data transfer between two platform requires a common data format. JSON is a common global format to send cross platform data.
两个平台之间的数据传输需要通用的数据格式。JSON 是一种通用的全局格式,用于发送跨平台数据。
drawChart(600/50, JSON.parse('<?php echo json_encode($day); ?>'), JSON.parse('<?php echo json_encode($week); ?>'), JSON.parse('<?php echo json_encode($month); ?>'), JSON.parse('<?php echo json_encode(createDatesArray(cal_days_in_month(CAL_GREGORIAN, date('m',strtotime('-1 day')), date('Y',strtotime('-1 day'))))); ?>'))
This is the answer to your question. The answer may look very complex. You can see a simple example describing the communication between server side and client side here
这是你的问题的答案。答案可能看起来非常复杂。你可以看到一个简单的例子描述了服务器端和客户端之间的通信这里
$employee = array(
"employee_id" => 10011,
"Name" => "Nathan",
"Skills" =>
array(
"analyzing",
"documentation" =>
array(
"desktop",
"mobile"
)
)
);
Conversion to JSON format is required to send the data back to client application ie, JavaScript. PHP has a built in function json_encode(), which can convert any data to JSON format. The output of the json_encode function will be a string like this.
需要转换为 JSON 格式才能将数据发送回客户端应用程序,即 JavaScript。PHP 有一个内置函数 json_encode(),可以将任何数据转换为 JSON 格式。json_encode 函数的输出将是这样的字符串。
{
"employee_id": 10011,
"Name": "Nathan",
"Skills": {
"0": "analyzing",
"documentation": [
"desktop",
"mobile"
]
}
}
On the client side, success function will get the JSON string. Javascript also have JSON parsing function JSON.parse() which can convert the string back to JSON object.
在客户端,成功函数将获取 JSON 字符串。Javascript 也有 JSON 解析函数 JSON.parse() ,它可以将字符串转换回 JSON 对象。
$.ajax({
type: 'POST',
headers: {
"cache-control": "no-cache"
},
url: "employee.php",
async: false,
cache: false,
data: {
employee_id: 10011
},
success: function (jsonString) {
var employeeData = JSON.parse(jsonString); // employeeData variable contains employee array.
});