在JS中从该数组中获取数据 [英] Getting data out of this array in JS
问题描述
最后,我从PHP中获得了一些输出,而不仅仅是单词 array。这花了我一天时间,AAAAHHHH。
Finally i get some output out of PHP instead of just the word "array". This took me a day, AAAAHHHH.
(请参见解决方案)
(see here for the solution)
因此,这是XMLHTTP请求(对PHP)的返回值,通过JS中的回调。我在PHP中使用 print_r
了。
So this is the return value from a XMLHTTP request (to PHP), via a callback in JS. I got it with print_r
in PHP.
我在此处发布了一段结果。
我的新问题:
I post a snippet of the results here. My new question:
- 此数据结构是由什么构成的?
-
如何从该结构中获取元素,例如CourseID(在JS中)?
- what is this data structure made of?
how to get elements out of this structure, such as CourseID (in JS)?
[0] => Parse\ParseObject Object
(
[serverData:protected] => Array
(
[CourseID] => DEMO2
[EndDate] => DateTime Object
(
[date] => 2017-03-31 10:26:00.000000
[timezone_type] => 2
[timezone] => Z
)
[InfoTitle1] => Welcome
[InfoText1] => Welcome to your course, called "sense & sales".
[Admin] => Remco@Demo1
)
我的PHP代码是
function getGroups(){
$query = new ParseQuery("CourseInfo");
$query->equalTo("Admin", "Remco@Demo1");
$results = $query->find();
// echo $results // this lead to the string "array"
//print_r($results); // this leads to the complicated array
//echo "<script>\r\n var phpOutput = " . json_encode($results) . ";\r\n console.log(phpOutput);\r\n</script>";
// this leads to [{},{}];
}
推荐答案
您想要的是JavaScript对象,因此您需要使用JSON。
What you want is a JavaScript Object, so you need to use JSON.
(JSON表示JavaScript对象表示法。)
(JSON means JavaScript Object Notation.)
PHP有一个 json_encode
函数,它将为您完成工作:( http://php.net/manual/en/function.json-encode.php )
PHP has a json_encode
function, which will do the work for you: (http://php.net/manual/en/function.json-encode.php)
json_encode —返回值的JSON表示形式
json_encode — Returns the JSON representation of a value
尝试以下操作:
PHP:
echo "<script>\r\n var phpOutput = " . json_encode($output) . ";\r\n console.log(phpOutput);\r\n</script>";
在浏览器中按F12键可在开发人员工具中打开浏览器的控制台日志窗口,以查看新的JavaScript对象
Press F12 in your browser to open the browser's Console log window in Developer Tools to see the new JavaScript Object.
这篇关于在JS中从该数组中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!