2011-04-14 63 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;