1

I have this PHP code that creates an array to display as JSON:

$return_arr = array();
$sql="SELECT * from prices ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs)) {
    $return_arr[] = array('label' => $result["product"], 'id' => $result["sequence"]);
}

currently, it displays like:

var data = [{"label":"VoIP Telephone Numbers","id":"3"},{"label":"VoIP Port Submit","id":"4"}];

but i need to get the quotes (") removed from the label and id

user2710234
  • 3,177
  • 13
  • 36
  • 54

2 Answers2

0

This is the regex I use for this task:

$not_valid_json = preg_replace(array('%([{,])(\s*)"([a-z0-9]+)"\s*:%i','%\\\\/%','%"%'), array('$1$3:','/','\''), json_encode($value));

Be aware though that this is not really valid JSON anymore according to the spec, but it will still evaluate in JavaScript.

Also note that this doesn't always work great if the value being encoded contains quotes.

Scott Jungwirth
  • 6,105
  • 3
  • 38
  • 35
0

You're not clear on how or where you're displaying the JSON, but all you probably need to do it use the JSON.parse method. Below is from my Chrome javascript console:

> var foo = JSON.parse('{"label":"VoIP Telephone Numbers","id":"3"}');
undefined
> foo
Object {label: "VoIP Telephone Numbers", id: "3"}
halabuda
  • 386
  • 1
  • 11