ajax,以json的形式检索多维php数组。

时间:2022-11-05 21:29:01

An Ajax call is computing me a multidimensional PHP array '$tree', after this call is made I would like to retrieve this array as a JSON.

一个Ajax调用正在为我计算一个多维PHP数组“$tree”,在这个调用之后,我想以JSON的形式检索这个数组。

My PHP page is only printing the Array:

我的PHP页面只打印数组:

print_r(json_encode($tree));

Here the success part of my ajax call:

这里是ajax调用的成功部分:

success: function(data, textStatus, jqXHR) {
    var myObject = JSON.parse(data);
    alert(myObject);
}

Unfortunately the alert box is not triggered.

不幸的是,警报框没有被触发。

Also, I noticed that when I'm reaching my PHP page through a web browser:

另外,我注意到当我通过web浏览器到达PHP页面时:

print_r(json_encode($tree)); 

isn't displaying anything, while:

没有显示任何东西,而:

print_r($tree);

Is printing my multidimensional Array

打印多维数组吗

4 个解决方案

#1


1  

echo json_encode($tree);
exit;

This is the ideal way to send JSON back to browser. Also check that you do not have any special characters in your array otherwise you will have to use BITMASKs in json_encode function.

这是将JSON发送回浏览器的理想方式。还要检查数组中没有任何特殊字符,否则必须在json_encode函数中使用位掩码。

Also, All string data must be UTF-8 encoded, so you may want to correct the encoding of your data.

此外,所有字符串数据都必须是UTF-8编码的,因此您可能希望纠正数据的编码。

Further reading: http://php.net/manual/en/function.json-encode.php

进一步阅读:http://php.net/manual/en/function.json-encode.php

#2


1  

As others already said, the correct method to output a JSON from PHP is via the json_encode function.

正如其他人已经说过的,从PHP输出JSON的正确方法是通过json_encode函数。

echo json_encode($tree);

You should probably check the encode of the Array data; if you have any string they must be encoded in UTF-8. I suggest you to loop through the $tree Array and to force the encode via the utf8_encode function (you can find PHP docs here).

您可能应该检查数组数据的编码;如果有任何字符串,它们必须用UTF-8编码。我建议您遍历$tree数组并通过utf8_encode函数强制进行编码(您可以在这里找到PHP文档)。

If your Array has a depth of 2 levels you can try something like this, or some kind of array walk loop.

如果你的数组的深度是2层,你可以尝试这样的东西,或者某种数组遍历循环。

for (i = 0; i < count($tree); i++)

{
   for (j = 0; j < count($tree[$i]; j++)

   {
       utf8_encode($tree[$i][$j]);
   }
}

You probably just need to encode strings, so you should reduce the amount of data to encode if you can (i.e. you know that only 2 indexes in the whole array could be strings, or maybe could contain characters that need to be encoded).

您可能只需要对字符串进行编码,所以您应该尽可能减少要编码的数据量(例如,您知道整个数组中只有两个索引可以是字符串,或者可能包含需要编码的字符)。

#3


1  

Never forget the header!

永远不会忘记的头!

<?php
header("Content-Type: application/json");
echo json_encode($tree);

#4


0  

First of all you need to test if the function json_encode exists.

首先,您需要测试函数json_encode是否存在。

if (function_exists('json_encode')) print 'json_encode working';
else print 'json_encode not working';

If it doesn't, you need to enable it, see here https://*.com/a/26166916/5043552

如果没有,您需要启用它,请参见这里的https://*.com/a/26166916/5043552

Last, use print json_encode($tree) instead of print_r(json_encode($tree)).

最后,使用print json_encode($tree)代替print_r(json_encode($tree))。

#1


1  

echo json_encode($tree);
exit;

This is the ideal way to send JSON back to browser. Also check that you do not have any special characters in your array otherwise you will have to use BITMASKs in json_encode function.

这是将JSON发送回浏览器的理想方式。还要检查数组中没有任何特殊字符,否则必须在json_encode函数中使用位掩码。

Also, All string data must be UTF-8 encoded, so you may want to correct the encoding of your data.

此外,所有字符串数据都必须是UTF-8编码的,因此您可能希望纠正数据的编码。

Further reading: http://php.net/manual/en/function.json-encode.php

进一步阅读:http://php.net/manual/en/function.json-encode.php

#2


1  

As others already said, the correct method to output a JSON from PHP is via the json_encode function.

正如其他人已经说过的,从PHP输出JSON的正确方法是通过json_encode函数。

echo json_encode($tree);

You should probably check the encode of the Array data; if you have any string they must be encoded in UTF-8. I suggest you to loop through the $tree Array and to force the encode via the utf8_encode function (you can find PHP docs here).

您可能应该检查数组数据的编码;如果有任何字符串,它们必须用UTF-8编码。我建议您遍历$tree数组并通过utf8_encode函数强制进行编码(您可以在这里找到PHP文档)。

If your Array has a depth of 2 levels you can try something like this, or some kind of array walk loop.

如果你的数组的深度是2层,你可以尝试这样的东西,或者某种数组遍历循环。

for (i = 0; i < count($tree); i++)

{
   for (j = 0; j < count($tree[$i]; j++)

   {
       utf8_encode($tree[$i][$j]);
   }
}

You probably just need to encode strings, so you should reduce the amount of data to encode if you can (i.e. you know that only 2 indexes in the whole array could be strings, or maybe could contain characters that need to be encoded).

您可能只需要对字符串进行编码,所以您应该尽可能减少要编码的数据量(例如,您知道整个数组中只有两个索引可以是字符串,或者可能包含需要编码的字符)。

#3


1  

Never forget the header!

永远不会忘记的头!

<?php
header("Content-Type: application/json");
echo json_encode($tree);

#4


0  

First of all you need to test if the function json_encode exists.

首先,您需要测试函数json_encode是否存在。

if (function_exists('json_encode')) print 'json_encode working';
else print 'json_encode not working';

If it doesn't, you need to enable it, see here https://*.com/a/26166916/5043552

如果没有,您需要启用它,请参见这里的https://*.com/a/26166916/5043552

Last, use print json_encode($tree) instead of print_r(json_encode($tree)).

最后,使用print json_encode($tree)代替print_r(json_encode($tree))。