注意到整数值需要是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"},...
然后我使用以下代码删除密钥sctr
和amount
:
$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的密钥吗?
答案 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));