使用未定义常量 ARRAY_FILTER_USE_BOTH - 假设 'ARRAY_FILTER_USE_BOTH'
Use of undefined constant ARRAY_FILTER_USE_BOTH - assumed 'ARRAY_FILTER_USE_BOTH'
我使用 Codeigniter 作为框架并使用来自表单的输入参数创建如下所示的数组。
$data = array(
'generated_id' => $this->input->post('generated_id'),
'one' => $this->input->post('one'),
'two' => $this->input->post('two'),
'three' => $this->input->post('three'),
'modified_date' => date('Y-m-d H:i:s'),
'modified_user_id' => $this->session->userdata('user_id')
);
我想做的是,如果输入字段的值为空,我想将特定数组位置的值设置为 NULL。
我正在使用下面的函数来完成它。
$data = array_filter($data,function($value, $key){
if(empty($value))
return NULL;
else
return $value;
}, ARRAY_FILTER_USE_BOTH);
但我最终遇到以下错误。
Use of undefined constant ARRAY_FILTER_USE_BOTH - assumed 'ARRAY_FILTER_USE_BOTH'
有没有简单的方法来完成这个或者我该如何解决这个问题?
由于您根本没有在回调中使用 $key
,因此您也不需要 ARRAY_FILTER_USE_BOTH
标志。您的函数可以简化为:
$data = array_filter($data);
说真的,它做同样的事情。
What I want to do is if the value from the input fields are empty I want to set the value of the particular array position to NULL
.
array_filter
一开始不会那样做。它将完全删除 "empty" 个元素,而不是将它们设置为 null
。你想要:
$data = array_map(function ($value) { return $value ?: null; }, $data);
这会将 falsey(空字符串、0
、空数组)的任何值设置为 null
。
我使用 Codeigniter 作为框架并使用来自表单的输入参数创建如下所示的数组。
$data = array(
'generated_id' => $this->input->post('generated_id'),
'one' => $this->input->post('one'),
'two' => $this->input->post('two'),
'three' => $this->input->post('three'),
'modified_date' => date('Y-m-d H:i:s'),
'modified_user_id' => $this->session->userdata('user_id')
);
我想做的是,如果输入字段的值为空,我想将特定数组位置的值设置为 NULL。 我正在使用下面的函数来完成它。
$data = array_filter($data,function($value, $key){
if(empty($value))
return NULL;
else
return $value;
}, ARRAY_FILTER_USE_BOTH);
但我最终遇到以下错误。
Use of undefined constant ARRAY_FILTER_USE_BOTH - assumed 'ARRAY_FILTER_USE_BOTH'
有没有简单的方法来完成这个或者我该如何解决这个问题?
由于您根本没有在回调中使用 $key
,因此您也不需要 ARRAY_FILTER_USE_BOTH
标志。您的函数可以简化为:
$data = array_filter($data);
说真的,它做同样的事情。
What I want to do is if the value from the input fields are empty I want to set the value of the particular array position to
NULL
.
array_filter
一开始不会那样做。它将完全删除 "empty" 个元素,而不是将它们设置为 null
。你想要:
$data = array_map(function ($value) { return $value ?: null; }, $data);
这会将 falsey(空字符串、0
、空数组)的任何值设置为 null
。