使用$ http post将对象发送到PHP数组键=>值

时间:2015-07-04 13:11:46

标签: php arrays angularjs post associative-array

我试图将一个对象发送到PHP机智POST并将其转换为关联数组

    postData: function(word, description, translate) {
        var formData = {
            w: word, 
            d: description, 
            t: trasnlate
        };

        $http({ 
            method: 'POST', 
            url: 'db.php', 
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: formData
        }).
            success(function(data, status, headers, config) {
                console.log(data);
            }).
            error(function(data, status, headers, config) {

            });
    }

db.php中

print_r($_POST); 

的console.log(数据):

 Array(
[{"w":"word","d":"description","h":"translate"}] => )

我想要这样的事情:

 Array(
[w] => word
[d] => description
[t] => translate

1 个答案:

答案 0 :(得分:1)

在PHP代码中执行:

some_list = [1,2,3,4,5]
print(some_list)
for each in some_list:
    some_list[each] = 4
    print("some_list["+str(each)+"] = 4")
    print(some_list)

[1, 2, 3, 4, 5]
some_list[1] = 4
[1, 4, 3, 4, 5]
some_list[4] = 4
[1, 4, 3, 4, 4]
some_list[3] = 4
[1, 4, 3, 4, 4]
some_list[4] = 4
[1, 4, 3, 4, 4]
some_list[4] = 4
[1, 4, 3, 4, 4]
如果POST有效负载中提供了正确的JSON对象,

$ array 将是一个关联数组。