Echo 不会显示从 json_decode() 检索到的任何值

发布于 2024-11-18 11:40:52 字数 1194 浏览 2 评论 0原文

我有一个非常简单的 JSON 对象,如下所示:

var data =
{
    "id" : 1
}

然后我在 PHP 中对其进行解码:

$decoded_data = json_decode(stripslashes($_POST['data']));  //this works
$id = intval($decoded_data->id); //in my debugger this is equal to 1 as expected

然后我继续将 $id 变量传递到一个查询数据库并返回一组“子活动”的函数中

$sub_activities = alp_get_all_sub_activities($id);  //this function works as expected and returns the correct result set

现在我有了对于指定的 $id 的子活动,我尝试使用循环访问它们:

foreach ($sub_activities as $activity) {
    echo __("<td><a id='" . $activity->id . "' href='' title='Activity'><div style=' border: 3px solid purple; width: 200px; height: 200px; overflow: scroll;'>" . $activity->name . "<br />" . $activity->id . "<br />" . $activity->description) . "</div></a></td>";
}

我的问题是:当 $id 设置为 intval($decoded_data->id) 时 echo 不显示任何内容,但是当我硬编码 $id = 1 时则显示所有内容按预期工作并显示在我的浏览器。我不太确定如何解决这个问题,因为我的调试器告诉我,当我设置 $id = intval($decoded_data->id); 时$id 等于 1。我可以用这个数字进行算术运算,它的行为似乎与任何整数一样,但由于某种原因 echo 和 print() 不会显示任何内容。

如果有人有任何见解,我非常感谢您的意见。

I have an extremely simple JSON object that looks like the following:

var data =
{
    "id" : 1
}

I then decode this in PHP:

$decoded_data = json_decode(stripslashes($_POST['data']));  //this works
$id = intval($decoded_data->id); //in my debugger this is equal to 1 as expected

I then proceed to pass the $id variable into a function that queries the database and returns a set of 'Sub Activities'

$sub_activities = alp_get_all_sub_activities($id);  //this function works as expected and returns the correct result set

Now that I have the Sub Activities for the designated $id, I attempt to access them using a loop:

foreach ($sub_activities as $activity) {
    echo __("<td><a id='" . $activity->id . "' href='' title='Activity'><div style=' border: 3px solid purple; width: 200px; height: 200px; overflow: scroll;'>" . $activity->name . "<br />" . $activity->id . "<br />" . $activity->description) . "</div></a></td>";
}

My problem is: echo displays nothing when $id is set to intval($decoded_data->id), but when I hardcode $id = 1 then everything works as expected and shows in my browser. I'm not quite sure how to approach this problem, because my debugger is telling me that when I set $id = intval($decoded_data->id); that $id is equal to 1. I can do arithmetic with this number and it seems like it behaves as any integer would, but for some reason echo and print() will not display anything.

If anyone has any insight I'd really appreciate your input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来世叙缘 2024-11-25 11:40:52

尝试像这样访问该值:

$id = $decoded_data->{'id'};
$id = (int)$decoded_data->{'id'};
$id = intval($decoded_data->{'id'});
$id = (int)$decoded_data->id;

而不是这样:

$id = intval($decoded_data->id);

再考虑一下 JSON 是否将整数视为布尔值?你能尝试一下吗

var data =
{
    "id" : "1"
}

try accessing the value like this:

$id = $decoded_data->{'id'};
$id = (int)$decoded_data->{'id'};
$id = intval($decoded_data->{'id'});
$id = (int)$decoded_data->id;

Instead of this:

$id = intval($decoded_data->id);

Thinking about this a little more is JSON treating the integer as a boolean? Could you try

var data =
{
    "id" : "1"
}
以为你会在 2024-11-25 11:40:52

事实证明,这是安装了 Buddypress 的 Wordpress 中 ajax 的问题。 Buddypress 出于自身目的将请求发送到其他地方。我刚刚创建了自己的 javascript 命名空间对象并重新路由了数据。以下教程帮助我解决了这个问题:

http://codex.wordpress.org/AJAX_in_Plugins

http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

JSON 现在可以完美运行。感谢您的帮助!

This turned out to be a problem with ajax in Wordpress with Buddypress installed. Buddypress was sending the request elsewhere for its own purposes. I just created my own javascript namespace object and rerouted the data. Here are tutorials that helped me figure this out:

http://codex.wordpress.org/AJAX_in_Plugins

http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

JSON now works perfectly. Thanks for the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文