How to remove slash inverted in json?

1

When executing this code:

    $array =array();
    $sqli=mysqli_connect('localhost', 'dbuser', 'pass', 'db');
    $query = mysqli_query($sqli,"SELECT * FROM usuarios");
    while ($fila = $query->fetch_assoc()) {
        $res=array('nombre'=>$fila["nombre"],'tipo'=>$fila["tipo"] 'contraseña'=>$fila["user"]);
        array_push($array,$res);
    }
    header("Content-type:application/json");
    echo json_encode($array);

I get this result:

{"nombre":"admin","tipo":"1","contrase\u00f1a":"$2y$10$H5GU7rN38jo3n\/eJW6KMHuovnCFYymi8GrQpshuhowWxYVZdq8r2S"}]

I generate inverted slashes after a / and I change the ñ for its html entity How can I fix this?

    
asked by iuninefrendor 14.12.2017 в 02:00
source

1 answer

1

Use the JSON_UNESCAPED_SLASHES option so that the / does not escape.

As for the ñ you are converting it to unicode, you can also force it to not convert it with JSON_UNESCAPED_UNICODE

// JSON_UNESCAPED_SLASHES => 64
// JSON_UNESCAPED_UNICODE => 256
// JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE => 320


$arrayName = array('Contraseña' => '$2y$10$H5GU7rN38jo3n/eJW6KMHuovnCFYymi8GrQpshuhowWxYVZdq8r2S');
echo json_encode($arrayName, 320);
// {"Contraseña":"$2y$10$H5GU7rN38jo3n/eJW6KMHuovnCFYymi8GrQpshuhowWxYVZdq8r2S"}
echo json_encode($arrayName);
// {"Contrase\u00f1a":"$2y$10$H5GU7rN38jo3n\/eJW6KMHuovnCFYymi8GrQpshuhowWxYVZdq8r2S"}

More information on link

    
answered by 14.12.2017 / 02:11
source