php json_encode奇怪问题说明,使用json_encode的朋友可以参考下。
json_encode 只支持utf-8格式这个就不多说了
代码如下:
$array = array (
[0] => array ( [sale_unit_detail_id] => 13 [price] => 740000 [geometric_lat] => 51.50348620 [geometric_lng] =>-0.01710900 )
[1] => Array ( [sale_unit_detail_id] => 27 [price] => 740000 [geometric_lat] => 51.50348620 [geometric_lng] => -0.01710900 )
);
上面的php数组json_encode 之后通常会转化php数组会转化为形如[[...][...]] 的格式,但是今天碰到一个怪问题,当我的数组长度达到142的时候 给我转化为了形如
{'0' :{..},'1':{...}......}的格式很奇怪,json_encode 一个神奇的函数
php中json_encode转数组为json字符串时的超奇怪问题。 对比以下两段代码:
代码如下:
$menu[0][0] = "title1";
$menu[0][1] = "bar1";
$menu[0][2] = "bar2";
$menu[1][0] = 'title2';
$menu[2][0] = 'title3';
echo '&$menu='.json_encode($menu).'&';
这个会输出:&$menu=[["title1","bar1","bar2"],["title2"],["title3"]]&
代码如下:
$menu[0]["title"] = "title1";
$menu[0]["bar1"] = "bar1";
$menu[0]["bar2"] = "bar2";
$menu[1][0] = 'title2';
$menu[2][0] = 'title3';
echo '&$menu='.json_encode($menu).'&';
而这个居然输出:&$menu=[{"title":"title1","bar1":"bar1","bar2":"bar2"},["title2"],["title3"]]&
什么意思?问题就在于{}和[],{}是对象,[]是数组!!!!在非php的情况下遍历的时候方法就不一样了!json_encode这个东西太神奇了,神奇起来让人有点郁闷!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn