这是我在php中的数据:
$userLimit = array(
"default" => array(
"playlistLimit" => 1,
"mediaLimit" => 2,
),
"editor" => array(
"playlistLimit" => 5,
"mediaLimit" => 20,
),
"author" => array(
"playlistLimit" => 2,
"mediaLimit" => 10,
),
);
当我在php中循环这个时,我得到:
foreach ($userLimit as $k => $v) {
var_dump($k);
}
string(7) "default" string(6) "editor" string(6) "author"
这是正确的,这正是我想要的。
当我用javascript编辑它(并发送到服务器端)时,我会这样做:
var userLimit = [
{
"default": {
"playlistLimit": "1",
"mediaLimit": "2"
}
},
{
"editor": {
"playlistLimit": "5",
"mediaLimit": "20"
}
},
{
"author": {
"playlistLimit": "2",
"mediaLimit": "10"
}
}
]
那么我需要在发送服务器端时对其进行序列化:
JSON.stringify(userLimit)
问题是,当它返回并在php中再次循环时,我会得到密钥:
foreach ($userLimit as $k => $v) {
var_dump($k);
}
int(0) int(1) int(2)
因为数据现在看起来是这样的:
array(3) { [0]=> array(1) { ["default"]=> array(2) { ["playlistLimit"]=> string(1) "1" ["mediaLimit"]=> string(1) "2" } } [1]=> array(1) { ["editor"]=> array(2) { ["playlistLimit"]=> string(1) "5" ["mediaLimit"]=> string(2) "20" } } [2]=> array(1) { ["author"]=> array(2) { ["playlistLimit"]=> string(1) "2" ["mediaLimit"]=> string(2) "10" } } }
如何使数据在保存后不更改格式?