Php 在插入SQL时保留JSON数据的顺序
我使用jquery序列化了多个输入字段,然后使用AJAX调用将数据保存到SQL数据库中。当我序列化输入字段时,序列化JSON数据的顺序如下所示:Php 在插入SQL时保留JSON数据的顺序,php,jquery,mysql,sql,json,Php,Jquery,Mysql,Sql,Json,我使用jquery序列化了多个输入字段,然后使用AJAX调用将数据保存到SQL数据库中。当我序列化输入字段时,序列化JSON数据的顺序如下所示: [{ "value1": [ "123", "111", "222", "333" ], "value2": [ "111", "222", "333", "444" ], "value3": [ "1111", "2222", "3333", "4444" ], "title1": [ "This is a title" ], "
[{
"value1": [ "123", "111", "222", "333" ],
"value2": [ "111", "222", "333", "444" ],
"value3": [ "1111", "2222", "3333", "4444" ],
"title1": [ "This is a title" ],
"title2": [ "Another title" ]
}]
[
{ "value1": [ "123", "111", "222", "333" ] },
{ "title1": "This is a title" },
{ "value2": [ "111", "222", "333", "444" ] },
{ "title2": "Another title" },
{ "value3": [ "1111", "2222", "3333", "4444" ] }
]
虽然我希望它是这样的:
[{
"value1": [ "123", "111", "222", "333" ],
"value2": [ "111", "222", "333", "444" ],
"value3": [ "1111", "2222", "3333", "4444" ],
"title1": [ "This is a title" ],
"title2": [ "Another title" ]
}]
[
{ "value1": [ "123", "111", "222", "333" ] },
{ "title1": "This is a title" },
{ "value2": [ "111", "222", "333", "444" ] },
{ "title2": "Another title" },
{ "value3": [ "1111", "2222", "3333", "4444" ] }
]
序列化代码的行如下所示:
var inputVals = $(this).closest('.blockContent').find("select, textarea, input").serialize();
将数组编码为JSON格式的PHP代码:
parse_str($request->inputVals,$inputVals);
$inputVals = array($inputVals);
$inputVals = json_encode($inputVals);
序列化的输入示例:
<input name="value1[0]" value="{{ $jsonValues['value1'][0] }}" type="number">
<input name="value1[1]" value="{{ $jsonValues['value1'][1] }}" type="number">
<input name="title1" value="{{ $jsonValues['value1'] }}" type="text">
更新:
为了更好地表述我的问题,我如何从Jquery/javascript使上面的输入字段变得像JSON结构一样,格式可以像上面的JSON代码中我想要的那样排序?javascript,因此JSON对象是无序的。两者之间没有任何区别
[{
"value1": [ "123", "111", "222", "333" ],
"title1": [ "This is a title" ],
"value2": [ "111", "222", "333", "444" ],
"title2": [ "Another title" ],
"value3": [ "1111", "2222", "3333", "4444" ]
}]
及
但是,数组是有序的。如果您可以将数据结构更改为
[{
"value1": [ "123", "111", "222", "333" ],
"title1": [ "This is a title" ]
}, {
"value2": [ "111", "222", "333", "444" ],
"title2": [ "Another title" ]
}, {
"value3": [ "1111", "2222", "3333", "4444" ],
"title3": "..."
}]
你会发现秩序得到了保留。每个对象可能首先有它们的值或标题,但每个值和标题都会一起出现。是的,这是我所理解的,但我正在努力通过serialize获得该结构,你知道我将如何重做代码,使结构像这样还是我在问题中写的最后一块吗?请编辑你的问题,并向我们显示$jsonValues的完整值。