我有一些来自查询的数字,我必须使用json_encode函数表示。 一切正常但输出看起来像这样
{ "label": "man", "data":[["0","1.13"], ["1","1.38"], ["2","1.87"], ["3","1.12"], ["4","1.28"]]}
所以我认为问题是所有数字都存储为字符串。 是否有转换所有元素的函数?
答案 0 :(得分:4)
您可能希望将JSON_NUMERIC_CHECK添加到json_encode函数中:
json_encode($array, JSON_NUMERIC_CHECK);
答案 1 :(得分:1)
您可以使用以下函数获取变量的整数或浮点值:
echo (integer)$variable;
echo (float)$variable;
答案 2 :(得分:-1)
<?php
$json = '{ "label": "man", "data":[["0","1.13"], ["1","1.38"], ["2","1.87"], ["3","1.12"], ["4","1.28"]]}';
$structure = json_decode($json, true);
$newData = $structure['data'];
for ($x=0;$x<count($newData);$x++):
for ($i=0;$i<count($newData[$i]);$i++):
$newData[$x][$i] = (float)$newData[$x][$i];
endfor;
endfor;
$structure['data'] = $newData;
print json_encode($structure);
新结果:
{"label":"man","data":[[0,1.13],[1,1.38],[2,1.87],[3,1.12],[4,1.28]]}