我想使用复选框来指定要添加到数组中的项,我的代码如下:
$columns = array(
if (isset($_POST['CustomerID'])) {
'Customer ID' => 'CustomerID',
}
if (isset($_POST['First_name'])) {
'First Name' => 'First_name',
}
);
我怎样才能做到这一点?非常感谢
您不能在数组内部使用if语句,请像这样将其移到外部。
$columns = array();
if (isset($_POST['CustomerID'])) {
$columns['Customer ID'] = 'CustomerID';
}
if (isset($_POST['First_name'])) {
$columns['First Name'] = 'First_name';
}
if (isset($_POST['CustomerID'])) {
array_push($columns, 'Customer ID');
}
// OR for those that like slight performance increases
if (isset($_POST['CustomerID'])) {
$columns[] = 'Customer ID';
}
你可以试试这个吗?您不会有自定义索引,您必须寻址$columns[0]
、$columns[1]
等,但最终它只包含您需要的内容。