- 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;
- }
- //该片段来自于http://www.codesnippet.cn/detail/090920135756.html
来源: http://www.codesnippet.cn/detail/090920135756.html