【发布时间】:2015-01-12 21:49:51
【问题描述】:
我有一个带有多维输入字段的复杂表单;
<input type="hidden" name="product[12267][ID]" value="76">
<input type="radio" name="product[12267][electrical_support]" value="1">
<input type="radio" name="product[12267][electrical_support]" value="0" checked="">
<input type="checkbox" name="product[12267][drinks]" value="1" >
<input type="radio" name="product[9999][electrical_support]" value="1">
<input type="hidden" name="product[9999][ID]" value="58">
使用jquery的ajax和serializeArray将表单发送到php;
var formData = new FormData();
formData.append('action', 'add_to_cart');
formData.append('products', $(form).serializeArray());
这会在控制台中输出一个对象数组,例如:
0: Object
name: "product[12267][starting_price]"
value: "15"
1: Object
name: "product[12267][ID]"
value: "79"
...
我的问题是如何根据 ID 将多个对象(在 js 中或在 php 中)分组/排列成单个产品数组,例如;
$products[12267] = array('starting_price' => 15, 'ID' => 79);
希望我能很好地解释这个案例,如果我需要提供更多详细信息,请告诉我
【问题讨论】:
标签: php jquery arrays forms serialization