[英]convert php array of multidimensional arrays to javascript array
Hi i have that php code example : 嗨,我有那个PHP代码示例:
$array1 = array("fruits" => "banana","vegetables" => "tomatos");
$array2 = array("name" => "Jack","Age" => "32");
$array3 = array($array1, $array2);
echo json_encode($array3);
javascript code includes jquery : javascript代码包括jquery:
var json_data = $.ajax({
type: 'POST',
url: 'scripts/myfile.php',
data: { action: 'myaction' },
dataType: 'json',
cache: false,
success: function(result) {
console.log(result);
}
});
how to convert json_data to an array and affect the two of $array1 & $array2 to javascript arrays ! 如何将json_data转换为数组并影响$ array1和$ array2的两个到javascript数组!
this is the json_data content : 这是json_data内容:
"{"HUM":[{"label":"2014-10-16 17:08:55","y":"58"},{"label":"2014-10-15 08:16:55","y":"56"},{"label":"2014-10-15 08:16:50","y":"56"},{"label":"2014-10-15 08:16:45","y":"56"},{"label":"2014-10-15 08:16:40","y":"56"},{"label":"2014-10-15 08:16:35","y":"56"},{"label":"2014-10-15 08:16:30","y":"56"},{"label":"2014-10-15 08:16:25","y":"56"},{"label":"2014-10-15 08:16:20","y":"56"},{"label":"2014-10-15 08:16:15","y":"56"},
"TEMP":[{"label":"2014-10-16 17:08:55","y":"26"},{"label":"2014-10-15 08:16:55","y":"24"},{"label":"2014-10-15 08:16:50","y":"24"},{"label":"2014-10-15 08:16:45","y":"24"},{"label":"2014-10-15 08:16:40","y":"24"},{"label":"2014-10-15 08:16:35","y":"24"},{"label":"2014-10-15 08:16:30","y":"24"},{"label":"2014-10-15 08:16:25","y":"24"},{"label":"2014-10-15 08:16:20","y":"24"},{"label":"2014-10-15 08:16:15","y":"24"},
"HUM2":[{"label":"2014-10-16 17:08:55","y":"38"},{"label":"2014-10-15 08:16:55","y":"36"},{"label":"2014-10-15 08:16:50","y":"36"},{"label":"2014-10-15 08:16:45","y":"36"},{"label":"2014-10-15 08:16:40","y":"36"},{"label":"2014-10-15 08:16:35","y":"36"},{"label":"2014-10-15 08:16:30","y":"36"},{"label":"2014-10-15 08:16:25","y":"36"},{"label":"2014-10-15 08:16:20","y":"36"},{"label":"2014-10-15 08:16:15","y":"36"},{"label":"2014-10-15 08:16:10","y":"36"},
"TEMP2":[{"label":"2014-10-16 17:08:55","y":"23"},{"label":"2014-10-15 08:16:55","y":"24"},{"label":"2014-10-15 08:16:50","y":"24"},{"label":"2014-10-15 08:16:45","y":"24"},{"label":"2014-10-15 08:16:40","y":"24"},{"label":"2014-10-15 08:16:35","y":"24"},{"label":"2014-10-15 08:16:30","y":"24"},{"label":"2014-10-15 08:16:25","y":"24"},{"label":"2014-10-15 08:16:20","y":"24"},{"label":"2014-10-15 08:16:15","y":"24"},{"label":"2014-10-15 08:16:10","y":"24"},{"label":"2014-10-15 08:16:05","y":"24"},{"label":"2014-10-15 08:16:00","y":"24"}]}"
now i want to convert it to array and then serparate arrays HUM - TEMP - HUM2 - TEMP2 现在我想将其转换为数组,然后分解为数组HUM-TEMP-HUM2-TEMP2
In your code, 在您的代码中
$array3 = array(array1, array2);
here array1 and array2 considered as string without quotes, so you will not get your answer. 在这里,array1和array2被视为不带引号的字符串,因此您将无法获得答案。 This should be PHP
variable. 这应该是PHP
变量。 You have to include $
sign in this. 您必须在其中包含$
符号。
$array3 = array($array1, $array2);
From jQuery $.ajax() documentation on dataType
when it is set to json
. 从dataType
jQuery $ .ajax()文档(设置为json
。
"json": Evaluates the response as JSON and returns a JavaScript object. “ json”:将响应评估为JSON并返回一个JavaScript对象。 The JSON data is parsed in a strict manner; JSON数据是严格解析的。 any malformed JSON is rejected and a parse error is thrown. 任何格式错误的JSON都会被拒绝,并引发解析错误。 As of jQuery 1.9, an empty response is also rejected; 从jQuery 1.9开始,空响应也被拒绝; the server should return a response of null or {} instead. 服务器应返回null或{}的响应。 (See json.org for more information on proper JSON formatting.) (有关正确的JSON格式的更多信息,请参见json.org。)
The object result is already a JSON object in the success function. 对象结果已经是成功函数中的JSON对象。 So you can simply use result[0]['fruits']
or result[1]['name']
to access the relevant arrays. 因此,您可以简单地使用result[0]['fruits']
或result[1]['name']
访问相关数组。
Edit : Corrected index as @charlietfl pointed out. 编辑 :更正了@charlietfl指出的索引。
$array3 = array(array1, array2);
should be $array3 = array($array1, $array2);
应该是$array3 = array($array1, $array2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.