dongzaizai2015 2013-11-06 14:12
浏览 162
已采纳

使用foreach循环将表单值插入到数组中

i have this long form that requires multiple inputs, how can i properly insert these inputs into an array using a foreach loop?

assuming i have these:

$name = $_POST['name'];
$color = $_POST['color'];
$age= $_POST['age'];
$gender = $_POST['gender'];
$location = $_POST['location'];
...etc..

instead of typing such this..

$myarray = array('id'=>$id,'name'=>$name,'color'=>$color,'age'=>$age,'gender'=>$gender,'location'=>$location,etc...);

how can i let foreach loop insert the appropriate values into the array?

  • 写回答

3条回答 默认 最新

  • doupai6875 2013-11-06 14:19
    关注

    all you need is this

    $myarray = array();
    foreach ($_POST as $key => $value){
       $myarray[$key] = $value;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?