/**
* Customised general propose helper class
*
class GeneralHelper
{
/**
* Cast array to object, available for multi-dimensional array
*
* @param mixed|array
* @return mixed|array{obj}
*/
static function arrayToObject($array) {
if(!is_array($array)) {
return $array;
}
$object = new stdClass();
if (is_array($array) && count($array) > 0) {
foreach ($array as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$object->$name = self::arrayToObject($value);
}
}
return $object;
}
else {
return FALSE;
}
}
* Customised general propose helper class
*
* @copyright 2012 Toowards -- tta project
* @author Jason
class GeneralHelper
{
/**
* Cast array to object, available for multi-dimensional array
*
* @param mixed|array
* @return mixed|array{obj}
*/
static function arrayToObject($array) {
if(!is_array($array)) {
return $array;
}
$object = new stdClass();
if (is_array($array) && count($array) > 0) {
foreach ($array as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$object->$name = self::arrayToObject($value);
}
}
return $object;
}
else {
return FALSE;
}
}
}
include_once( 'GeneralHelper' );
$arr = array('a'=>'A','x'=>array('b'=>'B'));
$obj = GeneralHelper::arrayToObject($arr);
var_dump($obj);
=============================
array 'a' => string 'A' (length=1) 'x' => array 'b' => string 'B' (length=1)
object(stdClass)[134] public 'a' => string 'A' (length=1) public 'x' => object(stdClass)[131] public 'b' => string 'B' (length=1)