(澳门新葡京官方网站) json 大括号保存对象,为什么关联数组在json中表现为对象,用{}包裹关联数组的数据?
澳门新葡京
澳门新葡京
当前位置 : 澳门新葡京 > 澳门新葡京

(澳门新葡京官方网站) json 大括号保存对象,为什么关联数组在json中表现为对象,用{}包裹关联数组的数据?

(澳门新葡京官方网站)

实验数据:

//关联数组
$result_array1[msg] = 13;
$result_array1[infor] = 24;
//索引数组
$result_array2[] = 1;
$result_array2[] = 2;
//索引数组
$arr = array0,1,3;
$arr[0] = $result_array1;
$arr[1] = $result_array2;
$arr[2] = $result_array1;
$arr[3] = 123;
$arr[4] = 3243;

$result_array[msg] = 13;
$result_array[infor] = 24;
$result_array[ra1] = $arr;

var_dump$result_array;
var_dumpjson_encode$result_array;

浏览器输出:

 array size=3
  msg => int 13
  infor => int 24
  ra1 => 
    array size=5
      0 => 
        array size=2
          msg => int 13
          infor => int 24
      1 => 
        array size=2
          0 => int 1
          1 => int 2
      2 => 
        array size=2
          msg => int 13
          infor => int 24
      3 => int 123
      4 => int 3243
string {"msg":13,"infor":24,"ra1":[{"msg":13,"infor":24},[1,2],{"msg":13,"infor":24},123,3243]} length=88

补充:

//关联数组
$result_array1[msg] = 13;
$result_array1[infor] = 24;
//索引数组
$result_array2[] = 1;
$result_array2[] = 2;
//索引数组
$arr = array0,1,3;
$arr[0] = $result_array1;
$arr[1] = $result_array2;
$arr[2] = $result_array1;
$arr[3] = 123;
$arr[4] = 3243;

$result_array[] = 13;
$result_array[] = 24;
$result_array[] = $arr;

var_dump$result_array;
var_dumpjson_encode$result_array;

结果:

array size=3
  0 => int 13
  1 => int 24
  2 => 
    array size=5
      0 => 
        array size=2
          msg => int 13
          infor => int 24
      1 => 
        array size=2
          0 => int 1
          1 => int 2
      2 => 
        array size=2
          msg => int 13
          infor => int 24
      3 => int 123
      4 => int 3243
D:\wamp\www\test\array.php:47:string [13,24,[{"msg":13,"infor":24},[1,2],{"msg":13,"infor":24},123,3243]] length=68

实验结论:关联数组json表现为{},索引数组表现为:[]
这个不是和json定义违背了吗?:

数组(在中括号中)

对象(在大括号中)

难道关联数组在PHP中是对象,而索引数组就不是对象,那它们的区别是什么????

  • 3月17日提问
  • 评论
  • 邀请回答
  • 编辑
    A string is a sequence of zero or more Unicode characters [UNICODE].
   Note that this citation references the latest version of Unicode
   rather than a specific release.  It is not expected that future
   changes in the UNICODE specification will impact the syntax of JSON.

   An object is an unordered collection of zero or more name/value
   pairs, where a name is a string and a value is a string, number,
   boolean, null, object, or array.

   An array is an ordered sequence of zero or more values.

http://www.faqs.org/rfcs/rfc7...

对象:无序的一系列值
数组:有序的一系列值

这是json的标准说明,所以没毛病

  • 3月17日回答
  • 2 评论
  • 编辑

没毛病啊
{"msg":13,"infor":24,"ra1":[{"msg":13,"infor":24},{"msg":13,"infor":24},{"msg":13,"infor":24},123,3243]}
ral的值是数组,所以用[]包含。
然后值的前三个又是一个json对象

  • 3月17日回答
  • 评论
  • 编辑

json_encode只将索引数组(indexed array)转为数组格式,而将关联数组(associative array)转为对象格式.

  • 3 天前回答
  • 评论
  • 编辑

栏目列表

广告位

澳门新葡京