将数组格式化为JSON以输入Google Charts API?

时间:2015-04-06 14:40:36

标签: javascript php arrays json google-chartwrapper

注意到整数值需要是json中的整数,但我使用的是字符串。

原始数组是:

   '[{"sctr":"Asset Managers","amount":"1586500"},{"sctr":"Auto 
    Parts","amount":"1618000"},{"sctr":"Business Support 
    Services","amount":"1012020"},{"sctr":"Coal","amount":"1043550"},
    {"sctr":"Consumer Finance","amount":"2285000"},...

然后我使用以下代码删除密钥sctramount

        $sector_final = array();
        array_push($sector_final, array("Sector", "Amount"));
        foreach( $sector_data as $row){
            array_push($sector_final, array_values($row));
        }
        var_dump(json_encode($sector_final));'

,并提供:

'[["Sector","Amount"],["Asset Managers","1586500"],["Auto
 Parts","1618000"],["Business Support Services","1012020"],
["Coal","1043550"],["Consumer Finance","2285000"],

但是,Google Chart Api的数量不得为引号。

我该怎么做呢,还有一种更有效的方法来删除json的密钥吗?

2 个答案:

答案 0 :(得分:3)

尝试将该特定元素投射到int中,然后再将其投入/播放:

$sector_final = array();
array_push($sector_final, array("Sector", "Amount"));
foreach( $sector_data as $row){
    $row['amount'] = (int) $row['amount']; // cast it
    array_push($sector_final, array_values($row));
}

答案 1 :(得分:1)

$sector_final = array();

    //array_push($sector_final, array("Sector", "Amount"));
    foreach( $sector_data as $row){

        $final = array("Sector" => "' . $row['sector'] . '",
                       "Amount" => $row['amount']);

        array_push($sector_final, $final);
    }
    var_dump(json_encode($sector_final));