我有一个输出数组(print_r):
Array
(
[a] => 706
[b] => 194
[c] => 164
[d] => 44
[e] => 42
[f] => 41
[g] => 40
[h] => 38
[i] => 13
)
我需要将其转换为这种格式的 json:
[{"name":"a","value":706},{"name":"b","value":194},{"name":"c","value":164},{"name":"d","value":44},{"name":"e","value":42},{"name":"f","value":41},{"name":"g","value":40},{"name":"h","value":38},{"name":"i","value":13}]
这是我尝试过的,但我只是猜测并没有得到正确的输出:
echo '[';
foreach($sortedArray as $key => $value) {
echo $dataTable = json_encode(array(
'name' => $key,
'value' => $value
), JSON_NUMERIC_CHECK);
};
echo ']';