2 つの初期フィールドがあるアプリケーションがあります。
- 名前
- 価格
フォームは次のようになります。
<form>
<input type="hidden" name="type" value="television" />
<label>Name <input type="text" name="name[]" /></label>
<label>Price <input type="text"" name="price[]" /></label>
</form>
現在、ユーザーはフォームに「さらにフィールドを追加」できますが、これは問題なく機能します。たとえば、誰かが [さらに追加] ボタンをクリックすると、フォームは次のようになります。
<form>
<input type="hidden" name="type" value="television" />
<label>Name <input type="text" name="name[]" /></label>
<label>Price <input type="text"" name="price[]" /></label>
<label>Name <input type="text" name="name[]" /></label>
<label>Price <input type="text"" name="price[]" /></label>
</form>
そして、さらに名前/価格を追加できます。私が遭遇している問題は、最初の価格フィールドを名前フィールドに関連付けることができないことです. 私はajaxを使用してデータを投稿していますが、それもうまく機能しています。
現在、投稿を var_dump すると、配列は次のようになります。
array(3) {
["type"]=>
string(10) "television"
["name"]=>
array(2) {
[0]=>
string(8) "name one"
[1]=>
string(8) "name two"
}
["price"]=>
array(2) {
[0]=>
string(9) "price one"
[1]=>
string(9) "price two"
}
}
私が必要としているのは、配列の値をマージして次のようにする機能です。
array(
"name" => "name one",
"price" => "price one",
"type" => "television"
)
array(
"name" => "name two",
"price" => "price two",
"type" => "television"
)
どんな助けでも大歓迎です!