PHP数组、对象相互转换
PHP的数组、对象相互转换
数组转为对象
/* * 数组转成对象 * */ function arrayToObject($array) { if (is_array($array)) { $obj = new StdClass(); foreach ($array as $key => $val){ $obj->$key = $val; } } else { $obj = $array; }
return $obj; }
对象转为数组
/* * 对象转为数组 * */ function objectToArray($object) { if (is_object($object)) { foreach ($object as $key => $value) { $array[$key] = $value; } } else { $array = $object; }
return $array; }