Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用复选框指定数组中的项_Php_Arrays_Forms - Fatal编程技术网

Php 使用复选框指定数组中的项

Php 使用复选框指定数组中的项,php,arrays,forms,Php,Arrays,Forms,我想使用复选框指定要添加到数组中的项,我的代码如下: $columns = array( if (isset($_POST['CustomerID'])) { 'Customer ID' => 'CustomerID', } if (isset($_POST['First_name'])) { 'First Name' => 'First_name', } ); 我怎样才能做到这一点? 非常感谢 if (isset($_POST['CustomerID']))

我想使用复选框指定要添加到数组中的项,我的代码如下:

$columns = array(
if (isset($_POST['CustomerID'])) {
   'Customer ID' => 'CustomerID', 
}
  if (isset($_POST['First_name'])) {
     '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]
等,但最终它只包含您需要的内容。

您不能在数组中使用if语句,请将其像这样移出数组

$columns = array();

if (isset($_POST['CustomerID'])) {
    $columns['Customer ID'] = 'CustomerID';
}

if (isset($_POST['First_name'])) {
    $columns['First Name'] = 'First_name';
}

这里什么不起作用?不要将条件语句放在数组语法中?你应该检查手册,特别是
使用方括号语法创建/修改部分:我不能将条件语句放在数组语法中。但我需要自定义索引。为什么要否决-(应该是这样的:如果(isset($\u POST['CustomerID']){$columns['Customer ID']=$\u POST['CustomerID'];}可以工作,但我确实可以使用自定义索引,因为我使用它们作为列的标题。没有办法吗?然后查看此帖子:并将数组\u push()替换为数组\u put\u位置()并添加相关参数。