将数组格式化为 JSON 以输入到 Google 图表 API?
Formatting an array as JSON for input into Google Charts API?
注意到 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 图表 Api 正常工作,金额不得包含在引号中。
我该怎么做,还有没有更有效的方法来删除 json 的密钥?
尝试在 feeding/puhsing 之前将该特定元素转换为 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));
}
$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));
注意到 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 图表 Api 正常工作,金额不得包含在引号中。
我该怎么做,还有没有更有效的方法来删除 json 的密钥?
尝试在 feeding/puhsing 之前将该特定元素转换为 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));
}
$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));