对象我正在寻找方法来数组转换成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;
您可以: