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

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

PHP中文网
Release: 2016-06-01 14:32:58
Original
971 people have browsed it


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;
    }
Copy after login

                   

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template