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

4
  • 1
    Why do you need the quotes removed from label? Commented Jun 2, 2014 at 16:44
  • stackoverflow.com/questions/13523729/… Commented Jun 2, 2014 at 16:46
  • Do you mean removed from the keys or the values? You cannot have the value of label without quotes (its a string) and the keys should be in quotes also Commented Jun 2, 2014 at 16:46
  • removed from the keys Commented Jun 2, 2014 at 16:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.