gpt4 book ai didi

php - 使用 jQuery.parseJSON 检索包含空格的 php 数组

转载 作者:行者123 更新时间:2023-11-29 08:25:57 24 4
gpt4 key购买 nike

当我检索没有空格的整数或字符串时,一切正常。但是当我检索包含空格和其他字符的描述等字段时,JavaScript 停止运行。

我的 javascript(与我的 php 位于同一文件中):

$(document).ready(function(){
var x = new Array();
x = jQuery.parseJSON('<?php echo json_encode($tt) ?>');

$("#ty").html(x[0]["dnp"]);
});

我的PHP:

$sql = mysql_query("SELECT * FROM D") or die (mysql_error());

$num_rows = mysql_num_rows($sql);

if ($num_rows > 0)
{
while($row = mysql_fetch_array($sql))
{
$dd = $row['Description'];
if (!(isset($tt)) || (count($tt)<1))
{
$tt = array(0 => array("dnp" => $dd));
}
else
{
array_push($tt , array("dnp" => $dd));
}
}
}

我错过了什么?

最佳答案

json 是 JavaScript。您不需要将其作为字符串提供,然后对其进行解析。

x = <?php echo json_encode($tt) ?>;

这应该可以。

关于php - 使用 jQuery.parseJSON 检索包含空格的 php 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917963/

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