创建PHP动态多字段的JSON格式

Posted

技术标签:

【中文标题】创建PHP动态多字段的JSON格式【英文标题】:Create JSON format of PHP dynamic multiple fields 【发布时间】:2021-01-25 10:21:57 【问题描述】:

我需要创建从 php 表单接收到的数据的特定 JSON 格式。如果我可以制作正确的数组,那么我可以将其转换为 JSON,但找不到如何...我的表单有多个字段,它们获取数据并转换为这样的数组

    Array
    (
        [user_id] => 
        [title2] => Billing
        [sub_title2] => to
        [emails2] => abc@gmail.com
        [sub_title3] => cc
        [emails3] => xyz@gmail.com
        [sub_title4] => for
        [emails4] => john@gmail.com
        [title5] => Free
        [sub_title5] => cc
        [emails5] => abc@gmail.com
        [sub_title6] => adf
        [emails6] => ben@gmail.com,ten@gmail.com
    )

这是获取数据的代码

    $(document).on("click", "#add_section", function (e) 
      i++; 
      k++;
    
     var html1 ='<div id="email_section'+k+'" style="margin-top: 20px" class="form-group row">';
                Html1 += '<div class="col-lg-12">';
                Html1 += '<div class="col-lg-9">';
                Html1 += '<label class="control-label col-lg-3">Add Section'+i+'<span style="color:red;"> *</span></label>';
                Html1 += '<div class="col-lg-9">';
                Html1 += '<input class=" form-control" id="title'+i+'[]" name="title'+i+'" type="text" placeholder="Sales" />';
                Html1 += '</div>';
                Html1 += '</div>';
                Html1 += '</div>';
                Html1 += '<div class="col-lg-12">';
                Html1 += '<div class="col-lg-9">';
                Html1 += '<label class="control-label col-lg-3"></label>';
                Html1 += '<div class="col-lg-2">';
                Html1 += '<input class=" form-control" id="sub_title'+i+'[]" name="sub_title'+i+'" type="text" placeholder="to" />';
                Html1 += '</div>';
                Html1 += '<div class="col-lg-7">';
                Html1 += '<input class=" form-control" id="emails'+i+'[]" name="emails'+i+'" placeholder="abc@gmail.com" />';
                Html1 += '</div>';
                Html1 += '</div>';
                Html1 += '<div class="col-lg-2">';
                Html1 += '<span class="btn btn-primary add_email_row" id="'+k+'">Add</span>';
                Html1 += '</div>';
                Html1 += '</div>';
    
      $('.modal-body').append(Html1); 
    
    
    );
    
    
    
    $(document).on("click", ".add_email_row", function (e) 
      i++; 
      k++;
    
      var section = $(this).attr("id");
    
      var Html = '<div id="row'+i+'" class="row">'; 
      Html += '<div class="col-sm-12">';
      Html += '<div class="col-sm-2" style="margin-left: 20px;"></div>';
      Html += '<div class="col-sm-9">';
      Html += '<div class="col-sm-2"><input class=" form-control" id="sub_title'+i+'[]" name="sub_title'+i+'" type="text" placeholder="to" /></div>';
      Html += '<div class="col-sm-7"><input class=" form-control" id="emails'+i+'[]" name="emails'+i+'" type="text" placeholder="emails" /></div>';
      Html += '<div class="col-sm-3"><a type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</a></div>';
      Html += '</div></div></div>';
    
    $('#email_section'+section+'').append(Html); 
    
    );

我想要的JSON格式是

    
       "billing":
          "to":"abc@email.com",
          "cc":"xyz@email.com",
          "adf":"mail@email.com",
          "from":""
       ,
       "free":
          "to":"abc@gmail.com,",
          "cc":"xyz@email.com",
          "adf":""
       
    

The UI of the PHP form for the understanding of the multiple dynamic fields

【问题讨论】:

【参考方案1】:

如果可以更改表单,则将输入更改为数组。还要封装你的两个块并将它们保持在相同的增量逻辑中:

<script>

function addSection(i, k)

    var Html1 =
    '<div id="email_section'+k+'" style="margin-top: 20px" class="form-group row">' +
        '<div class="col-lg-12">' +
            '<div class="col-lg-9">' +
                '<label class="control-label col-lg-3">Add Section'+i+'<span style="color:red;"> *</span></label>' +
                '<div class="col-lg-9">' +
                    '<input class=" form-control" id="title'+i+'" name="title['+i+']" type="text" placeholder="Sales" />' +
                '</div>' +
            '</div>' +
        '</div>' +
        '<div class="col-lg-12">' +
            '<div class="col-lg-9">' +
                '<label class="control-label col-lg-3"></label>' +
                '<div class="col-lg-2">' +
                    '<input class=" form-control" id="sub_title'+i+'" name="sub_title['+i+'][]" type="text" placeholder="to" />' +
                '</div>' +
                '<div class="col-lg-7">' +
                    '<input class=" form-control" id="emails'+i+'" name="emails['+i+'][]" placeholder="abc@gmail.com" />' +
                '</div>' +
            '</div>' +
            '<div class="col-lg-2">' +
                '<span class="btn btn-primary add_email_row" id="'+k+'">Add</span>' + 
            '</div>' +
        '</div>';

    $('.modal-body').append(Html1); 


function addEmail(obj, i, k)

    var section = obj.attr("id");
    var Html =
    '<div id="row'+i+'" class="row">' +
        '<div class="col-sm-12">' +
            '<div class="col-sm-2" style="margin-left: 20px;"></div>' +
            '<div class="col-sm-9">' +
                '<div class="col-sm-2"><input class=" form-control" id="sub_title'+i+'" name="sub_title['+i+'][]" type="text" placeholder="to" /></div>' +
                '<div class="col-sm-7"><input class=" form-control" id="emails'+i+'" name="emails['+i+'][]" type="text" placeholder="emails" /></div>' +
                '<div class="col-sm-3"><a type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</a></div>' +
            '</div>' +
        '</div>' +
    '</div>';
    
    $('#email_section'+section).append(Html); 

    
var i   =   0;
var k   =   0;
$(document).on("click", "#add_section,.add_email_row", function (e) 
    if($(this).hasClass('add_email_row')) 
        addEmail($(this), i, k);
    
    else 
        i++; 
        k++;
        addSection(i, k);
    
);

</script>

你应该留下:

Array
(
    [title] => Array
        (
            [1] => Section 1
            [2] => Section 2
        )

    [sub_title] => Array
        (
            [1] => Array
                (
                    [0] => subtitle1
                    [1] => subtitle2
                    [2] => subtitle3
                )

            [2] => Array
                (
                    [0] => subtitle1
                )

        )

    [emails] => Array
        (
            [1] => Array
                (
                    [0] => email1@example.com
                    [1] => email2@example.com
                    [2] => email3@example.com
                )

            [2] => Array
                (
                    [0] => email1@example.com
                )

        )

)

从这里可以更容易地在 php 中获取您的数组:

$array    =   $_POST;
$new    =   [];
foreach($array['title'] as $k => $title) 
    $new[$title]    =   array_combine($array['sub_title'][$k], $array['emails'][$k]);


echo json_encode($new, JSON_PRETTY_PRINT);

这会给你:

 
    "Section 1": 
        "subtitle1": "email1@example.com",
        "subtitle2": "email2@example.com",
        "subtitle3": "email3@example.com"
    ,
    "Section 2": 
        "subtitle1": "email1@example.com"
    

【讨论】:

非常感谢 Rasclatt...这正是我所需要的,真是太棒了

以上是关于创建PHP动态多字段的JSON格式的主要内容,如果未能解决你的问题,请参考以下文章

php 将PHP注册的ACF字段转换为可导入的JSON格式

php 将PHP注册的ACF字段转换为可导入的JSON格式

php 将PHP注册的ACF字段转换为可导入的JSON格式

php 将PHP注册的ACF字段转换为可导入的JSON格式

php静态动态对象及json格式

Logstash动态模板映射收集Nginx的Json格式的日志