更改由Ajax发布的关联数组的结构 [英] Change structure of associative array posted by Ajax

查看:76
本文介绍了更改由Ajax发布的关联数组的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Ajax正在发布使用关联数组创建的表单字段,并且一切正常,但是返回数据的结构不正确.

My Ajax is posting form fields created using an associative array and everything works, but the structure of the returned data isn't correct.

表单字段:

<input id="my_array[][system]" type="text" value="" name="my_array[0][system]">
<textarea id="my_array[][note]" type="text" value="" name="my_array[0][note]"></textarea>

<input id="my_array[][system]" type="text" value="" name="my_array[1][system]">
<textarea id="my_array[][note]" type="text" value="" name="my_array[1][note]"></textarea>

<input id="my_array[][system]" type="text" value="" name="my_array[2][system]">
<textarea id="my_array[][note]" type="text" value="" name="my_array[2][note]"></textarea>

Ajax调用:

$.ajax(ajaxurl, {
    type: "POST",
    dataType: "json",
    data: {
        action: "update_postmeta",
        post_id: post_id,
        nonce: nonce,
        my_array: (function () {
            var my_array = {};
            $('input:text[name^="my_array"], textarea[name^="my_array"]')
                    .each(function () {
                        my_array[this.name] = $(this).val();
                    });
            return my_array;
        })()
    },
    success: function (response) {
        alert(response);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert("Error: " + textStatus + '\r\n\r\n' + errorThrown);
    }
})

结果:

array(3) (
  [my_array[0] => array(2) (
    [system] => (string) Some system 1
    [note] => (string) Note 1
  )
  [my_array[1] => array(2) (
    [system] => (string) Some system 2
    [note] => (string) Note 2
  )
  [my_array[2] => array(2) (
    [system] => (string) Some system 3
    [note] => (string) Note 3
  )
)

我需要这样:

array(3) (
  [0] => array(2) (
    [system] => (string) Some system 1
    [note] => (string) Note 1
  )
  [1] => array(2) (
    [system] => (string) Some system 2
    [note] => (string) Note 2
  )
  [2] => array(2) (
    [system] => (string) Some system 3
    [note] => (string) Note 3
  )
)

解决方案在以下代码内:

The solution is within the following code:

my_array: (function () {
    var my_array = {};
    $('input:text[name^="my_array"], textarea[name^="my_array"]')
            .each(function () {
                my_array[this.name] = $(this).val();
            });
    return my_array;
})()

但是我的尝试只会使情况变得更糟.如何获得所需的结构?基本上,我需要第二个维度作为索引键,而不是'my_array [1'等".

But my attempts just break it worse. How do I get the desired structure? Basically, I need the second dimension to be index keys instead of 'my_array[1' etc.

推荐答案

为确保您在php端获得正确值的正确数字键,我认为您将必须通过常规提取输入名称.表达式并手动分配.

To ensure you get the right numeric keys for the right values on the php side, I think you are going to have to extract the names of the inputs via regular expressions and assign manually.

例如,在每个循环中:

var parsedName = this.name.match(/^my_array\[(\d+)\]\[(.*)\]$/);
my_array[parsedName[1]] = my_array[parsedName[1]] || {};
my_array[parsedName[1]][parsedName[2]] = $(this).val();

这篇关于更改由Ajax发布的关联数组的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆