上面的解释很清楚了,关于php里数组赋值的问题,列举如下:
<?php //对象 class JsonTest{ var $id = 1; var $name = 'heiyeluren'; var $gender = '男'; } $obj = new JsonTest; echo json_encode($obj)."<br />"; //{"id":1,"name":"heiyeluren","gender":"u7537"} //数字索引数组 $arr1 = array(1, 'heiyeluren', '男'); echo json_encode($arr1)."<br />"; //[1,"heiyeluren","u7537"] //关联索引数组 $arr2 = array("id"=>1,"name"=>'heiyeluren',"gender"=>'男'); echo json_encode($arr2)."<br />"; //{"id":1,"name":"heiyeluren","gender":"u7537"} //多维数字索引数组 $arr3 = array(array(1, 'heiyeluren', '男'), array(1, 'heiyeluren', '男')); echo json_encode($arr3)."<br />"; //[[1,"heiyeluren","u7537"],[1,"heiyeluren","u7537"]] //关联索引数组 $arr4 = array(array("id"=>1,"name"=>'heiyeluren',"gender"=>'男'), array("id"=>1,"name"=>'heiyeluren',"gender"=>'男')); echo json_encode($arr4)."<br />"; //[{"id":1,"name":"heiyeluren","gender":"u7537"},{"id":1,"name":"heiyeluren","gender":"u7537"}] ?>
*(备注:关于在JS里调用传过来的数组的问题。可以用object的方式:obj.id、obj.name但是更多时候,它显示给我undefined,所以更推荐大家用数组的方式:obj[id]、obj[name] )