Home >php教程 >PHP源码 >数组中的索引关联数据全部统一转换成json对象

数组中的索引关联数据全部统一转换成json对象

PHP中文网
PHP中文网Original
2016-06-01 14:32:58970browse


public static function encode(&$var)
    {
        return '{'.implode(',',self::encodeExcute($var)).'}';
    }

    private static function encodeExcute(&$var)
    {
        $json = array();
        switch (gettype($var))
        {
            case 'array':
                foreach($var as $key=>$value)
                {
                    if(is_array($value))
                    {
                        $json[] = '"'.$key.'":{'.implode(',',self::encodeExcute($value)).'}';
                        //$json[$key] = self::encode($value);
                    } elseif (is_object($value)) {
                        $json[] = "\"{$key}\":".json_encode($value->attributes);
                    } else {
                        echo 'ERROR 未知类型,还未解析';
                        Yii::app()->end();
                    }
                }
                break;
            case 'object':
                break;
        }
        return $json;
    }

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn