数组到对象在kohana

| 我正在寻找在kohana中将数组转换为ojbect的方法  我懂了 http://docs.kohanaphp.com/helpers/arr#to_object 但是此方法在kohana 3.1中不起作用。 此功能的新替代方法是什么?     
已邀请:
您可以使用PHP的
type casting
手动完成操作(Type转换具有较低的标头):
$array = array(\'a\' => \'c\', \'b\' => \'d\');
$obj = (object)$array;

echo $obj->a; // c
    
如果是一维数组,只需使用
$obj = (object)$array;
将其转换为
object
    
您可以覆盖Arr类。 创建文件APPPATH / classes / arr.php: 添加新方法:
class Arr extends Kohana_Arr {

public static function to_object(array $array, $class = \'stdClass\')
{
        $object = new $class;
        foreach ($array as $key => $value)
        {
                if (is_array($value))
                {
                // Convert the array to an object
                        $value = arr::to_object($value, $class);
                }
                // Add the value to the object
                $object->{$key} = $value;
        }
        return $object;
}
}     

要回复问题请先登录注册