将一个stdClass对象转化为数组
php的stdClass相当于一个Object
function object_to_array($obj)
{
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val)
{
$val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}
将一个数组转化为stdClass
function array_to_object($arr){
$object = new stdClass;
foreach($arr as $key=>$value){
$object->$key = $value;
}
return $object;
}
测试下
echo "\r\n";
$arr = array();
$arr['a'] = 10;
$arr['b'] = 22;
$arr['c'] = 23;
$arr['d'] = 26;
$sd = array_to_object($arr);
print_r($sd);
$arr = object_to_array($sd);
print_r($arr);