php数组与对象之间的转换

//对象转数组
function object_array($object) {
    $object = (array)$object;
    foreach ($object as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $object[$k] = (array)$this->object_array($v);
        }
    }

    return $object;
}
//数组转对象
function array_object($array) {
    if (gettype($array) != 'array') {
        return;
    }
    foreach ($array as $k => $v) {
        if (gettype($v) == 'array' || getType($v) == 'object') {
            $array[$k] = (object)$this->array_object($v);
        }
    }

    return (object)$array;
}

你可能感兴趣的:(php)