我有一个包含一些项的数组。有些是字符串,另一些是int,这些是在mysql中用类型(int,varchar)定义的。我创建的数组如下:
(由于太长而缩小了数组)
[3] => Array
(
[account_id] => *******
[month_id] => 201903
[month_comment] => 4,5% spend flat
[month_spend] => 23000
[month_budget] => 0
[month_adops] => 1035
[month_adops_forecast] => 1035
)
[4] => Array
(
[account_id] => ******
[month_id] => 201905
[month_comment] =>
[month_spend] =>
[month_budget] => 0
[month_adops] =>
[month_adops_forecast] => 45
)
[5] => Array
(
[account_id] => *******
[month_id] => 201906
[month_comment] =>
[month_spend] =>
[month_budget] => 0
[month_adops] =>
[month_adops_forecast] => 92
)
就像你看到的
"month_spend"
和/或
"month_adops"
为空,问题是在PHP中,当数据库中的字段为整数时,值被转换为“空”,因此结果给出:
Incorrect integer value: '' for column 'month_spend' at row 2"
所以我试着在foreach循环中改变这个:
$result_replace = array();
foreach ($data_replace as $key => $result) {
$result['month_id'] = substr_replace($result['month_id'], '2019', 0, 4);
if(empty($result['month_budget'])) {
$result['month_budget'] = 0;
$result_replace[] = $result;
}
else if(empty($result['month_spend'])) {
$result['month_spend'] = 0;
$result_replace[] = $result;
}
}
但看起来foreach循环没有编辑数据?