/** * 组合返回JSON数据格式 * @author wjh * @version 2014-5-10 * @param mixed $data string text or array * @param string $err error message * @param null $type 返回类型,json | jsonp * @param null $jsonpfunc 如果 $type= jsonp ,用来指定回调函数 * @throws Exception * @return array */ public static function encodeJsonReturn($data, $err = null, $type = 'json', $jsonpfunc = 'jsonpCallback') { if (is_null($data)) { $data = array(); } elseif (BArrayHelper::isCActiveRecord($data)) { $data = $data->attributes; } elseif (BArrayHelper::isCActiveRecordArray($data)) { $data = BArrayHelper::getModelAttributesArray($data); } else { //$data = array(); } if (is_array($data)) { $data = SnapshotHelper::encodeArray($data); } if (!empty($type) && $type == 'jsonp') { $str = sprintf('%s({"err":[%s],"data":%s})', $jsonpfunc, $err, $data); } else { $str = sprintf('{"err":[%s],"data":%s}', $err, $data); } return $str; }
/** * getAttributesArray * @param $data * @return array */ public static function print_r2($data) { if (BArrayHelper::isCActiveRecordArray($data)) { $data = self::getAttributesArray($data); } self::print_r($data); }