如何根据键名将一个数组分成多个

时间:2014-11-11 01:01:56

标签: php html arrays forms post

我正在生成表单字段,我假设我需要为每个表单字段设置不同的名称,因此我将每个单独循环迭代的ID添加到每个字段的名称中。

问题是POST提交数组全部被混合在一起,我不确定如何分离成具有单独子数组的数组:

传递每个字段名称具有特定ID的循环创建字段:

function display(arrayObject) {
    $token = md5(uniqid());
    $_SESSION['token'] = $token;

    echo('<form></table>');

    foreach($arrayObject as $field) {
        echo('<tr><td><a href="index.php?deletefield='.$field['id'].'&deletetoken='.$deletetoken.'">X</a></td>
            <td><input type="text" name="order'.$field['id'].'" value="'.$field['orderid'].'"></td>
            <td><input type="text" name="name'.$field['id'].'" value="'.$field['name'].'"></td>
            <td><input type="text" name="type'.$field['id'].'" value="'.$field['type'].'"></td>
            <td><input type="text" name="length'.$field['id'].'" value="'.$field['length'].'"></td>
            <td><center><input type="checkbox" name="required'.$field['id'].'" value="'.$field['required'].'" checked></center></td>
            <td><input type="text" name="description'.$field['id'].'" value="'.$field['description'].'"></td></tr>');
    }

    echo('<tr><td colspan="7"><br><center><input type="hidden" name="token" value="'.$token.'">
          <input type="submit" value="Save Changes"></td></tr>
          </table></form>');
}

返回POST数组:

Array ( 
    [order1] => 0 [name#1] => token [type1] => hidden [length1] => 100 [description1] => some text. 
    [order3] => 1 [name3] => firstname [type3] => text [length3] => 100 [required3] => 1 [description3] => first name 
    [order2] => 2 [name2] => lastname [type2] => text [length2] => 100 [required2] => 1 [description2] => last name 
    [token] => edc4e81c4391269886029896795d0e2d 
) 

我需要什么,所以我可以遍历这组数据:

Array ( 
    Array ( [order] => 0 [name] => token [type] => hidden [length] => 100 [description] => some text. ),
    Array ( [order] => 1 [name] => firstname [type] => text [length] => 100 [required] => 1 [description] => first name ),
    Array ( [order] => 2 [name] => lastname [type] => text [length] => 100 [required] => 1 [description] => last name ),
    Array ( [token] => edc4e81c4391269886029896795d0e2d )
) 

1 个答案:

答案 0 :(得分:2)

您可以设置分组名称属性以轻松管理这些输入,更像是行设置名称。

粗略的例子:

echo '
    <tr>
        <td>
            <a href="index.php?deletefield='.$field['id'].'&deletetoken='.$deletetoken.'">X</a>
        </td>
        <td>
            <input type="text" name="inputs['.$field['id'].'][order]" value="'.$field['orderid'].'" />
        </td>
        <td>
            <input type="text" name="inputs['.$field['id'].'][name]" value="'.$field['name'].'" />
        </td>
        <td>
            <input type="text" name="inputs['.$field['id'].'][type]" value="'.$field['type'].'" />
        </td>
        <td>
            <input type="text" name="inputs['.$field['id'].'][length]" value="'.$field['length'].'" />
        </td>
        <td>
            <center><input type="checkbox" name="inputs['.$field['id'].'][required]" value="'.$field['required'].'" checked /></center>
        </td>
        <td>
            <input type="text" name="inputs['.$field['id'].'][description]" value="'.$field['description'].'" />
        </td>
    </tr>
';

然后在PHP当然,像平常一样处理它:

$inputs = $_POST['inputs'];
foreach($inputs as $field_id => $values) {
    // handle things from here
    // $values['order'] and others
}