简体   繁体   English

在 JS 中从这个数组中获取数据

[英]Getting data out of this array in JS

Finally i get some output out of PHP instead of just the word "array".最后,我从 PHP 中得到了一些输出,而不仅仅是“数组”这个词。 This took me a day, AAAAHHHH.这花了我一天,AAAAHHHH。

(see here for the solution) (解决方法见这里

So this is the return value from a XMLHTTP request (to PHP), via a callback in JS.所以这是通过 JS 中的回调从 XMLHTTP 请求(到 PHP)的返回值。 I got it with print_r in PHP.我用 PHP 中的print_r得到了它。

I post a snippet of the results here.我在这里发布了一个结果片段。 My new question:我的新问题:

  • what is this data structure made of?这个数据结构是由什么组成的?
  • how to get elements out of this structure, such as CourseID (in JS)?如何从这个结构中获取元素,例如 CourseID(在 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 )

My PHP code is我的 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 [{},{}]; 
}

What you want is a JavaScript Object, so you need to use JSON.你想要的是一个 JavaScript 对象,所以你需要使用 JSON。

(JSON means JavaScript Object Notation.) (JSON 表示 JavaScript 对象表示法。)

PHP has a json_encode function, which will do the work for you: ( http://php.net/manual/en/function.json-encode.php ) PHP 有一个json_encode函数,它将为您完成工作:( http://php.net/manual/en/function.json-encode.php

json_encode — Returns the JSON representation of a value json_encode — 返回值的 JSON 表示

Try this:尝试这个:

PHP: PHP:

echo "<script>\r\n var phpOutput = " . json_encode($output) . ";\r\n console.log(phpOutput);\r\n</script>";

Press F12 in your browser to open the browser's Console log window in Developer Tools to see the new JavaScript Object.在浏览器中按 F12 以在开发人员工具中打开浏览器的控制台日志窗口以查看新的 JavaScript 对象。

You may want to convert your PHP object to JSON first via json_encode() .您可能希望首先通过json_encode()将 PHP 对象转换为 JSON。

This will allow you to easily work with it in JavaScript like you normally would with any JavaScript object.这将允许您在 JavaScript 中轻松使用它,就像您通常使用任何 JavaScript 对象一样。

I stopped trying to work with this array/object in JS.我停止尝试在 JS 中使用这个数组/对象。 But instead i managed it on the PHP side and sent the results to JS.但是我在 PHP 端管理它并将结果发送给 JS。

Now it is all simple.现在一切都很简单。 (-; (-;

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

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