Home  >  Article  >  Backend Development  >  php json_encode strange problem explanation_PHP tutorial

php json_encode strange problem explanation_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:23:49911browse

json_encode only supports utf-8 format. I won’t say much about this

Copy code The code is as follows:

$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 )
);

The above php array After json_encode, the php array is usually converted into a format like [[...][...]], but today I encountered a strange problem. When the length of my array reached 142, it was converted into a format like
 The format of {'0':{..},'1':{...}......} is very strange, json_encode is a magical function
json_encode in php to convert array Super strange problem when using json string.

Compare the following two pieces of code:
Copy the code The code is as follows:

$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).'&';

This will output: &$menu=[["title1","bar1","bar2"],["title2"],["title3"]]&
Copy code The code is as follows:

$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).'&';

And this actually outputs: &$menu=[{"title": "title1","bar1":"bar1","bar2":"bar2"},["title2"],["title3"]]&

What does it mean? The problem lies in {} and []. {} is an object and [] is an array! ! ! ! The method is different when traversing in non-php situations! json_encode is so amazing, it makes people a little depressed!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324404.htmlTechArticlejson_encode only supports utf-8 format. I won’t go into details about this. Copy the code. The code is as follows: $array = array ( [ 0] = array ( [sale_unit_detail_id] = 13 [price] = 740000 [geometric_lat] = 51.5034...
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