我正在 JS 中构建一个数组,如下所示:
var slots = {};
$(".taken").each(function(item) {
var key = $(this).attr("id");
slots[key] = "<?php echo $_SESSION['alias']; ?>";
});
var json = JSON.stringify(slots);
var date = "<?php echo $_GET['date']; ?>"
$.ajax({
type: "POST",
url: "controllers/dutyupdate2.php",
data:{ array : json, date: date },
success : function(response){
console.log (response)
}//end success
});//end ajax
在我发布的 PHP 脚本中,我需要对其进行解码以匹配以下格式:
array( 'D1P'=>"JohnC" , 'D6E' => "JohnC")
我得到:
Array(
[D2E] => JohnC
[D6E] => JohnC
[D3BU] => JohnC
)
无论我如何解码数组,我都会得到一个以我的键作为索引的索引数组。 我是否在 JS 代码中错误地构建了数组或错误地解码了?
提前致谢
最佳答案
这就是你想要的格式,只是显示方式不同。请参阅此 PHP 代码进行验证
$a = array( 'D1P'=>"JohnC" , 'D6E' => "JohnC");
print_r($a);
这给出
Array
(
[D1P] => JohnC
[D6E] => JohnC
)
作为输出。因此,无需尝试或搜索任何不同的东西。
关于php - JSON 对象到 PHP 关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14877958/