Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove double quote in json_encode()

I want remove double quote in my json_encode, that is my code:

<?php

require_once 'config.inc.php';
//## Clase Base de Datos
require_once 'Database.class.php';
//## Obtengo los parametros pasados por el metodo GET
$params = $_REQUEST;

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();

$result = mysql_query("SELECT * from ranking WHERE posicion BETWEEN     ".$params['pos_ini']." AND ".$params['pos_fi']) or die('Could not query');

if(mysql_num_rows($result)){
    $array_json=array();
    $filas = mysql_num_rows($result);
    $columnas = mysql_num_fields($result);

    for($i=0;$i<$filas;$i++)
    {
        $fila_dato = mysql_fetch_assoc($result);
        for($k=0;$k<$columnas;$k++)
        {
                    $campo = mysql_field_name($result,$k);
                    $campo = str_replace('\"', '', $campo);
                    $array_json[$i][$campo] = $fila_dato[$campo];
        }
    }
    $array_final = json_encode($array_json);
    $array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9]*)":/','$1:',$array_final);
    echo $array_final;
} else {
    echo '[]';
}
?>

My result is that:

[{"id_posiciones":"1",posicion:"1",nick:"biwer",puntos:"1000",uid:"1",pais:"ES",idioma:"ES","device_version":"4"}]

I want to remove double quote of "id_posiciones" and "device_version" too.

How can I do for the result is that:

[{id_posiciones:"1",posicion:"1",nick:"biwer",puntos:"1000",uid:"1",pais:"ES",idioma:"ES",device_version:"4"}]
like image 249
jordiAnd Avatar asked Dec 18 '12 17:12

jordiAnd


People also ask

How do I ignore double quotes in JSON?

if you want to escape double quote in JSON use \\ to escape it.

What does the PHP function json_encode () do?

The json_encode() function is used to encode a value to JSON format.

How do you escape a double quote in Postman?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters.

Does JSON always use double quotes?

JSON names require double quotes.


2 Answers

You can use this bellow code to remove quote from numeric value.

http://php.net/manual/en/json.constants.php

It will work >=PHP 5.3.

$encoded = json_encode($data, JSON_NUMERIC_CHECK);
like image 91
Vijaysinh Parmar Avatar answered Oct 05 '22 21:10

Vijaysinh Parmar


If you add an underscore to your regex at the end it will do it.

$array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:',$array_final);

I assume that's what that preg_replace is for anyway.

like image 32
Alex Avatar answered Oct 05 '22 20:10

Alex