在前端JS代码中,ajax提交如果写了dataType:"json",
并且传递的数据是经过JSON.stringify()处理后的json字符串data:JSON.stringify(inputdata),
在PHP接收后如果直接做json_decode,json_decode(file_get_contents("php://input"))
那么得到的是一个stdClass Object 即:对象。
在实际使用中,需要将对象转为数组。
方法如下:
1、json_decode时候增加参数true$data= json_decode($object); //得到的是对象object
$data= json_decode($object, ture); //得到的是数组array
2、将对象转为数组
2.1 简单转换$data= (array)json_decode($object); //得到的是数组array
2.2自定义方法//PHP stdClass Object转array
function object_array($test) {
if(is_object($array)) {
$array = (array)$array;
} if(is_array($array)) {
foreach($array as $key=>$value) {
$array[$key] = object_array($value);
}
}
return $array;
}
2.3使用get_object_vars()$array = get_object_vars($object);
2.4另一个方法$array = json_decode(json_encode(simplexml_load_string($xmlString)),TRUE);
2.5自定义方法2function object2array(&$object) {
$object = json_decode( json_encode( $object),true);
return $object;
}
3、直接调用对象使用$item = $object->item;