前端按钮触发js函数
函数中使用ajax的话
$.post(url,data,function(result){
},"JSON");
result就是json格式
有四种写法
①全索引数组:
$json = '[["gemma",22],["huqin",23]]'; $jsontoobj = json_decode($json); var_dump($jsontoobj);
②第一层索引,内层关联:
$json2 = '[{"name":"gemma","age":22},{"name":"huqin","age":23}]'; $jsontoobj2 = json_decode($json2,true); var_dump($jsontoobj2);
③第一层关联,内层索引:
$json3 = '{"stu1":["gemma",22],"stu2":["huqin",23]}'; $jsontoobj3 = json_decode($json3,true); var_dump($jsontoobj3);
④全关联数组:
$json4 = '{"stu1":{"name1":"gemma","age":22},"stu2":{"name":"huqin","age":23}}'; $jsontoobj4 = json_decode($json4,true); var_dump($jsontoobj4);
反正只要json格式数据有带{}的,如果想要转成数组,json_decode就需要第二个参数并且为true。
{"name":"protect Name"} 这是你写的运行后的代码 ,你这结果估计是跟你环境有关系把 你是不是环境配置的有问题
改成$array[member][yuanminghe][username]
<?php
$array_1= array();//一维数组;
$array_2= array();//二维数组;
$array_1['username']="dfdsf";
$array_1['age']=25;
$array_2['nember']['shjsh']['username']="dfdsf";
$array_2['nember']['shjsh']['age']=25;
$array_2['nember']['hjhj']['username']="ghgj";
$array_2['nember']['hjhj']['age']=27;
//print_r($array_2);
$jsonObj_1=json_encode($array_1);
echo $jsonObj_1;
?>
是你的标点符号有问题啊,不是中文下的‘ ’而是在英文下的' ',这个你要搞清楚啊!
最后得到的结果如下:
你是json_encode的时候被转成十六进制了?一般在encode之前用urlencode加密中文字符串,然后decode的时候,再用urldecode解出来~
相当不错,辛苦了
大概是执行效率的差别吧。单引号效率更好一些。但是在一般情况下,都体现不出来。