Reputation: 51
I have a problem on setting an array index using a ternary if operator. What I'm trying to do is, if a statement satisfies an if condition, I want to add an additional index to an array which I will be using to insert data from the database. However, every time I use the ternary if operator to include these array indices, I always get an error
Unexpected '=>' T_DOUBLE_ARROW
Here is my code:
$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'] : ''));
Anyone who knows how to solve this problem? Am I doing something wrong? Any help would be appreciated
Upvotes: 1
Views: 222
Reputation: 222
You can't selectively set indexes while defining an array (the way you are) but you can use array_filter to remove the unwanted indexes for you:
$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'] : '',
));
This way any empty string values in the array will be removed before being assigned to the $data variable.
For reference, see:
Upvotes: 1
Reputation: 26470
If you want to add data dynamically to an array, you shouldn't do it with ternary operators like that if you don't want the key to exist if the condition fails. Add them separately by checking on the condition after defining the array and add the elements if the condition is true.
$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'];
You can still use a ternary operator inside the array-definition, but then you would still create the keys (even though the value in that element is empty)
$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'] : ''));
Upvotes: 0
Reputation: 94672
Move the ternary if functionality to after the =>
like so
$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']
: ''
);
Upvotes: 0
Reputation: 62626
Here is the way you should use it:
$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'] : ''));
Upvotes: 0