实验数据:
//关联数组
$result_array1['msg'] = 13;
$result_array1['infor'] = 24;
//索引数组
$result_array2[] = 1;
$result_array2[] = 2;
//索引数组
$arr = array(0,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_dump(json_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 = array(0,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_dump(json_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中是对象,而索引数组就不是对象,那它们的区别是什么????