开发者

PHP 2d array problem

开发者 https://www.devze.com 2023-01-27 18:03 出处:网络
I\'m having some troubles with getting data from a form into a 2d array in PHP. Note that I\'m going to be lazy with the syntax here..

I'm having some troubles with getting data from a form into a 2d array in PHP.

Note that I'm going to be lazy with the syntax here..

<form1>
<name=basketcolor value=red>
<name=fruit1 value=apple><name=amount1 value=4>
<name=fruit2 value=banana><name=amount2 value=9>
<name=fruit3 value=pear><name=amount3 value=6>
<submit>


<fo开发者_如何学Gorm2>
<name=basketcolor value=green>
<name=fruit1 value=orange><name=amount1 value=8>
<name=fruit2 value=melon><name=amount2 value=1>
<name=fruit3 value=apple><name=amount3 value=3>
<submit>

Say that there are x number of forms with this configuration, but only 3 basket colors. If I were to express my 2d array in table form, it would look like this: (number of rows is actually zero at the start and will increase as each form is added)

         | Red | Green | Blue | Total |
|        |     |       |      |       |
|        |     |       |      |       |
|        |     |       |      |       |
|        |     |       |      |       |
|        |     |       |      |       |
|        |     |       |      |       |
|        |     |       |      |       |
|        |     |       |      |       |

Okay, user clicks on submit, say for form1. The table will become:

         | Red | Green | Blue | Total |
| Apple  |   4 |       |      |     4 |
| Banana |   9 |       |      |     9 |
| Pear   |   6 |       |      |     6 |

When we want to submit form2, notice it both forms contain 'apple'. I don't want it to create another row, again called apple but add to an existing row on the correct column.

         | Red | Green | Blue | Total |
| Apple  |   4 |     3 |      |     7 |
| Banana |   9 |       |      |     9 |
| Pear   |   6 |       |      |     6 |
| Orange |     |     8 |      |     8 |
| Melon  |     |     1 |      |     1 |

So with this, can someone please help with the 2d array coding required?


have a look at array_merge - if you have stored the first POST somewhere array_merge($OLD_POST,$_POST) should do more or less what you want...

0

精彩评论

暂无评论...
验证码 换一张
取 消