这是与此类似的问题:
Convert js Array() to JSon object for use with JQuery .ajax

除了我有一个包含多个数组的对象。

对象看起来像(模拟):

{“用户”:[1,2,3,4],“客户”:[5,6,7,8],“ CompletionStatus”:“ pastdue”,“ DateRange”:“ thisweek”}

并创建如下:

            Filter = new FilterData;

            Filter.Add(9, "Clients");
            Filter.Add(12, "Clients");
            Filter.Add(75, "Clients");

            Filter.Add(9, "Users");
            Filter.Add(12, "Users");
            Filter.Add(75, "Users");

            Filter.SetValue("DateRange", "yesterday");

    function FilterData(){

        this.Users = [];

        this.Clients = [];

        this.Options = [];
        this.Options.CompletionStatus = [];
        this.Options.DateRange = [];

        this.Add = function(id, type){
            this[type].push(id);
            this[type] = this[type].unique();
            return;
        }

        this.Rem = function(id, type){+
            this[type].splice( Filter[type].indexOf(id), 1);
            this[type] = this[type].unique();
            return;
        }

        this.SetValue = function(key, value){
            this.Options[key] = value;
        }

    }


...

如果我只是这样做:

AjaxRequest = $.ajax({
...
data: Filter,
...
});


似乎obj最终将像:... Users = 1&Users = 2&Users = 3&...。

这导致PHP仅为Users看到一个值,在本例中为3,它将是最后一个值。

我需要PHP正确查看数组的位置是:..Users [] = 1&Users [] = 2&Users [] = 3&...。

任何想法如何纠正这一点?

Example:

In firebug, my post looks like this:

Clients 1
Clients 10
CompletionStatus    pastdue
DateRange   14
Users   2
Users   3
Users   4

and my response looks like this:
page: <?php print_r($_POST) ?>

Array
(
    [Users] => 4
    [Clients] => 10
    [CompletionStatus] => pastdue
    [DateRange] => 14
)

最佳答案

在javascript中,将用户名更改为Users []。 'Users []'是javascript对象的有效属性名称:

var o= { 'Users[]': 'hello user' };
alert(o['Users[]']);

09-25 16:46
查看更多