/** * Converter objetos em array. * * @param type $var * @return type */ public static function objectToArray($var) { $result = array(); $references = array(); // loop over elements/properties foreach ($var as $key => $value) { // recursively convert objects if (is_object($value) || is_array($value)) { // but prevent cycles if (!in_array($value, $references)) { // Verificar se o valor é nulo. Não adiciona tuplas // vazias ao json if (!is_null($value) && !empty($value)) { $result[$key] = JsonUtil::objectToArray($value); $references[] = $value; } } } else { // Verificar se o valor é nulo. Não adiciona tuplas // vazias ao json if (!is_null($value) && !empty($value)) { // simple values are untouched $result[$key] = utf8_encode($value); } } } return $result; }
function echoRespnse($status_code, $response) { $slim = \Slim\Slim::getInstance(); // Http response code $slim->status($status_code); // setting response content type to json $slim->response()->header('Content-Type', 'application/json;charset=utf-8'); // Chamada ao método estático para conversão de caracter UTF-8. // Tranforma o array ou objeto em JSON. echo json_encode(JsonUtil::objectToArray($response)); //echo json_encode($response, JSON_HEX_QUOT | JSON_HEX_TAG); //echo json_encode($response, JSON_FORCE_OBJECT, true); //echo Zend_Json::encode($response); $slim->stop(); }