gpt4 book ai didi

javascript - PHP 多维数组到 JavaScript

转载 作者:行者123 更新时间:2023-12-03 02:49:30 24 4
gpt4 key购买 nike

我想在 Javascript 函数中打印 PHP 数组的数据...问题是,它不起作用。这就是 PHP 中数据的复合方式:

$daten = array();
$anzahl = array();
$leads = array();
if ($result = $this->databaseConnection->query($sql)) {
while ($row = $result->fetch_assoc()) {
$daten[] = $row["Datum"];
$anzahl[] = $row["Anzahl"];
}
$this->logger->lwrite('Leads: '. $leads);
$leads[] = array(array("daten" => $daten), array("anzahl" => $anzahl));
return json_encode($leads);
}

这就是 JavaScript POST 请求:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
xhttp.open("POST", "/requestLeadClicksController.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(jQuery('#formToRequestLeadClicks').serialize());

这是我在 console.log(this.responseText); 上得到的:

[[{"daten":["2017-12-21","2017-12-22","2017-12-23"]},{"anzahl":["1","2","1"]}]] 

以及 this.responseText.datenthis.responseText[0]this.responseText[0].datenthis.responseText[daten] 正在仅打印数据数组。我想要得到的只是这个:

"2017-12-21","2017-12-22","2017-12-23"

anzahl 数组也是如此。我也只想拥有这个:

"1","2","1"

我将不胜感激任何形式的帮助!亲切的问候!

最佳答案

您有一个包含对象数组的数组,其中包含字符串数组。要深入了解最里面的字符串数组,您需要两个数组索引,后跟一个对象属性。

datenArray = responseText[0][0].daten,
anzahlArray = responseText[0][1].anzahl;

let responseText = [[{"daten":["2017-12-21","2017-12-22","2017-12-23"]},{"anzahl":["1","2","1"]}]],
datenArray = responseText[0][0].daten,
anzahlArray = responseText[0][1].anzahl;

console.log( datenArray );
console.log( anzahlArray );

关于javascript - PHP 多维数组到 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com