简体   繁体   English

JSON对象到PHP关联数组

[英]JSON object to PHP associative array

I am building an array in JS as such: 我正在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

In my PHP script I am posting to, I need to decode it to match the following format: 在我发布的PHP脚本中,我需要解码它以匹配以下格式:

array( 'D1P'=>"JohnC" , 'D6E' => "JohnC")

I get: 我明白了:

Array(
    [D2E] => JohnC
    [D6E] => JohnC
    [D3BU] => JohnC
)

No matter how I decode the array, I get an indexed array with my key as the index. 无论我如何解码数组,我得到一个索引数组,我的键作为索引。 Am I building the array incorrectly in the JS code or decoding incorrectly? 我是在JS代码中错误地构建数组还是错误解码?

Thanks in advance 提前致谢

This is the format you want, just displayed differently. 这是您想要的格式,只是以不同的方式显示。 See this PHP code to verify 请参阅此PHP代码以进行验证

$a = array( 'D1P'=>"JohnC" , 'D6E' => "JohnC");
print_r($a);

this gives 这给了

Array
(
    [D1P] => JohnC
    [D6E] => JohnC
)

as output. 作为输出。 So, there's no need to try or search anything different. 因此,没有必要尝试或搜索任何不同的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM