制作这样的 json 字符串
{"1":"7","2":"14","3":"25","4":"11"}
In php
,当您将元素推入数组时,您必须传递一个键。例如
$teamMembers = [];
$teamMembers['1'] = 7;
$teamMembers['2'] = 14;
...
如果你不把它变成associative array
, php
转换为 json 字符串时会将其视为 json 数组..
Tip:你可以只传递一把钥匙,它也能起作用。
例如
$test = [1, 2, 3, 4, 5, 6];
$test['10'] = 56;
echo json_encode($test);
Output:
"{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"10":56}"
PHP 参考 http://php.net/manual/en/function.json-encode.php#refsect1-function.json-encode-notes