仅从数字JS中删除双qoutes

时间:2015-11-24 14:32:06

标签: php regex

我有以下对象:

[["Jan","0"],["Feb","0"],["Mar","0"],["Apr","0"],["May","0"],["Jun","0"],["Jul","0"],["Aug","86"],["Sep","211"],["Oct","223"],["Nov","147"],["Dec","149"]]

我需要它看起来像:

[["Jan",0],["Feb",0],["Mar",0],["Apr",0],["May",0],["Jun",0],["Jul",0],["Aug",86],["Sep","211],["Oct",223],["Nov",147],["Dec",149]]

我通过PHP json_encode($ var)生成它;我怎么能在Javascript中通过Regex删除数字周围的qoutes?

PHP

function specialtest()
{
    $data = [];
    $year = 2015;

    for ($i = 1; $i <= 12; $i++) {
        $sql = 'SELECT count(*) as count FROM reservations WHERE YEAR(start) = ' . $year . ' AND MONTH(start)=' . $i;
        $query = $this->db->query($sql);
        $results = $query->result_array();
        $data[] = [date('M', strtotime($year . '-' . $i . '-01')), $results[0]['count']];
    }
    echo json_encode($data);
}

1 个答案:

答案 0 :(得分:1)

你可以修改你的行:

$data[] = [date('M', strtotime($year . '-' . $i . '-01')), $results[0]['count']];

为:

$data[] = [date('M', strtotime($year . '-' . $i . '-01')), (int) $results[0]['count']];

这应该可以解决问题。

主题中的更多内容将在您的jsonized字符串上运行,如

preg_replace('/\"(\d)\"/', '\1', json_encode(array('a' => '1')))