2011-04-14 59 views

回答

6

您可以手动使用PHP的type casting(没有与类型转换下集管)做转换为object重写Arr类。

  1. 创建文件APPPATH /班/ arr.php:

  2. 添加新的方法:

    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; 
    } 
    

    }

0

如果它是一个一维数组,只是它使用$obj = (object)$array;