数组对象相互转化

function array2object($array) {

    if (is_array($array)) {
        $obj = new StdClass();
        foreach ($array as $key => $val){
            $obj->$key = $val;
        }
    } else { $obj = $array; }
    return $obj;
}


//PHP stdClass Object转array
function object_array($array)
{
if(is_object($array))
{
$array = (array)$array;
}
if(is_array($array))
{
foreach($array as $key=>$value)
{
$array[$key] = object_array($value);
}
}
return $array;
}


实测有效!

 

转载于:https://www.cnblogs.com/pansidong/p/11202848.html

你可能感兴趣的:(数组对象相互转化)