如何使用php在复选框中显示数组值?

时间:2016-04-20 12:36:49

标签: php arrays

我正在使用以下代码

$val= $module[$i]['subModule'];
$val1=$module[$i]['submoduleId'];
$valArr['name'] = explode(",", $val);
$valArr1['id']= explode(",", $val1);
$result=array_merge($valArr,$valArr1);
foreach($result as $name)
{
    if(!empty($name))
    {
        echo "<input type='checkbox' name='type[]' value='".$name['id']."'>".$name['name']." ";
    }
}

显示未定义的索引错误。

我打印$ result变量我得到了以下输出:

Array ( [name] => Array (
                  [0] => group
                  [1] => checkin
                  [2] => personalinfo 
                 ) 
         [id] => Array ( 
                  [0] => 3 
                  [1] => 2
                  [2] => 1 
                 )
      )

1 个答案:

答案 0 :(得分:4)

试用此代码:

foreach($result['name'] as $key => $name){
    echo "<input type='checkbox' name='type[]' value='".$result['id'][$key]."'>".$name." ";
}