HDTuto.com

PHP - How to remove double quotes from json array keys?


Some days ago, I was just working on my old PHP project and I need to remove double quotes from JSON array keys because I want to add it to my chart. first of all, i just print my array but it shows as a string with double quotes in array keys. So chart does not appear on my dashboard. I just found what-what was an issue but at last, i just found what is an issue after a long time. It's crazy when sometime we have a small issue and taking too much time... hahaha, but finally, i got I need to remove double quotes from the key.

So if you have the same requirement or you want to remove double quotes from string jquery then you can do it easily by following small example code.

You can see by just copy and paste bellow code and run with your php file what you will get output.

Example:

<?php


$myJsonString = '{"id":"1", "name":"HD Topi", "from":"2012-01-01 09:00", "to":"2012-01-01 10:00"}';


$myJsonString = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $myJsonString);


print_r($myJsonString);

Output:

{id:"1", name:"HD Topi", from:"2012-01-01 09:00", to:"2012-01-01 10:00"}

I hope you found your best solution...




March 9, 2018 | Category : PHP
Related Posts