【问题标题】:Unexpected T_DOUBLE_ARROW on ternary if operator三元 if 运算符上的意外 T_DOUBLE_ARROW
【发布时间】:2017-09-03 04:48:17
【问题描述】:

我在使用三元 if 运算符设置数组索引时遇到问题。我想要做的是,如果语句满足 if 条件,我想向数组添加一个附加索引,我将使用该索引从数据库中插入数据。但是,每次我使用三元 if 运算符来包含这些数组索引时,总是会出错

意外的'=>' T_DOUBLE_ARROW

这是我的代码:

$data = array('remark' => $this->input->post('remark'),
           'rating' => $this->input->post('rating'),
           ($_SESSION['user_type'] == 'Customer' ? 'user_id' => $_SESSION['id'] : ''),
           ($_SESSION['user_type'] == 'Customer' ? 'food_item_id' => $this->input->post['refid'] : ''));

谁知道如何解决这个问题?难道我做错了什么?任何帮助将不胜感激

【问题讨论】:

  • @Qirel 已经尝试了页面中提到的解决方案,但问题仍然存在
  • 因为双箭头在三元运算符里面。这不是动态向数组添加元素的方式。

标签: php arrays


【解决方案1】:

在定义数组时,您无法选择性地设置索引(按照您的方式),但您可以使用 array_filter 为您删除不需要的索引:

$data = array_filter(array(
    'remark' => $this->input->post('remark'),
    'rating' => $this->input->post('rating'),
    'user_id' => $_SESSION['user_type'] == 'Customer' ? $_SESSION['id'] : '',
    'food_item_id' => $_SESSION['user_type'] == 'Customer' ? $this->input->post['refid'] : '',
));

这样,数组中的任何空字符串值都将在分配给 $data 变量之前被删除。

参考见:

【讨论】:

    【解决方案2】:

    这是你应该使用它的方式:

    $data = array('remark' => $this->input->post('remark'),
               'rating' => $this->input->post('rating'),
               'user_id' => ($_SESSION['user_type'] == 'Customer' ?  $_SESSION['id'] : ''),
               'food_item_id' => ($_SESSION['user_type'] == 'Customer' ?  $this->input->post['refid'] : ''));
    

    【讨论】:

      【解决方案3】:

      将三元 if 功能移动到 => 之后,就像这样

      $data = array(
                  'remark'        => $this->input->post('remark'),
                  'rating'        => $this->input->post('rating'),
                  'user_id'       => $_SESSION['user_type'] == 'Customer' 
                                  ? $_SESSION['user_type'] 
                                  : '',
                  'food_item_id'  => $_SESSION['user_type'] == 'Customer' 
                                  ? $this->input->post['refid'] 
                                  : ''
                  );
      

      【讨论】:

        【解决方案4】:

        如果您想将数据动态添加到数组中,如果您不希望在条件失败时存在键,则不应使用三元运算符来执行此操作。通过检查定义数组的条件之后单独添加它们,如果条件为真则添加元素。

        $data = array('remark' => $this->input->post('remark'),
                      'rating' => $this->input->post('rating'));
        
        if ($_SESSION['user_type'] == 'Customer')
            $data['user_id'] = $_SESSION['id'];
        if ($_SESSION['user_type'] == 'Customer')
            $data['food_item_id'] = $this->input->post['refid'];
        

        您仍然可以在数组定义中使用三元运算符,但是您仍然会创建键(即使该元素中的值为空)

        $data = array('remark' => $this->input->post('remark'),
                      'rating' => $this->input->post('rating'),
                      'user_id' => ($_SESSION['user_type'] == 'Customer' ? $_SESSION['id'] : ''),
                      'food_item_id' => ($_SESSION['user_type'] == 'Customer' ? $this->input->post['refid'] : ''));
        

        【讨论】:

        • 我也尝试使用该解决方案,但是当我已经尝试在数据库中插入数据时,我收到了 Unknown field type 错误
        • 如果您需要密钥存在于数组中(未在问题中指定),那么您需要第二个 sn-p 代码。
        • 我指的是我收到Unknown field type 错误的第二个sn-p 代码
        • 虽然它与您最初提出的问题无关(您的问题被回答了 3 次),但这听起来可能是 $this->input->post['refid'] 应该是 $this->input->post('refid') 的问题,或者是您未显示的代码的某些部分。无论哪种方式,您提出的原始问题都已得到回答,您应该在这里接受一个答案,并为此提出一个新问题 - 因为这是一个不同的问题:-)
        猜你喜欢
        • 2017-03-05
        • 2011-12-31
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        • 2012-12-11
        相关资源
        最近更新 更多