简体   繁体   中英

Remove double quotes for json value

I'm trying to fetch data and transform this in json to use with High chart.

Here's my code:

$arr = $this->Company->find('all'); // fetch the array
    $arr1 = array();
    foreach ($arr as $value) {
        $tmp = array();

        $tmp['name'] = $value['Company']['nome'];
        $tmp['data'] = '['. count($value['Branch']) .']';

        $arr1[] = $tmp;
        }
    $json = json_encode($arr1);
    $json = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $json);
    debug($json);

And my atual json:

'[{name:"Sotreq",data:"[11]"},{name:"Somov",data:"[1]"},{name:"Soimpex",data:"[0]"}]'

I've used preg_replace to remove double quotes of keys. I want to remove double quotes for the data value too. Pls help?

Replace your data line with this one:

   $tmp['data'] =  array(count($value['Branch']));

What are you doing with the json on the other end? Normally you don't need to do any regular expression munging at all, this is the way of madness.

Double quotes in the key field is fine, and optional. It should work with or without the quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM