對象我正在尋找方法來數組轉換成ojbect在Kohana的 我得到這個Array對Kohana的
http://docs.kohanaphp.com/helpers/arr#to_object
但這種方法不Kohana的3.1工作。 什麼是這個功能的新選擇?
$array = array('a' => 'c', 'b' => 'd');
$obj = (object)$array;
echo $obj->a; // c
對象我正在尋找方法來數組轉換成ojbect在Kohana的 我得到這個Array對Kohana的
http://docs.kohanaphp.com/helpers/arr#to_object
但這種方法不Kohana的3.1工作。 什麼是這個功能的新選擇?
$array = array('a' => 'c', 'b' => 'd');
$obj = (object)$array;
echo $obj->a; // c
您可以手動使用PHP的type casting
(沒有與類型轉換下集管)做轉換爲object
重寫Arr類。
創建文件APPPATH /班/ 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;
}
}
如果它是一個一維數組,只是它使用$obj = (object)$array;
您可以: