我试图仅显示还包含天和月的字符串的年份,然后将数组中的datum
的值更新为仅显示年份。仅显示年份是没有问题的,而是代替了datum
值,而是添加了一个新数组。如何防止这种情况发生?
我现在的阵列:
Array
(
[0] => Array
(
[datum] => 2019
)
[1] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 932
[cid] => 202
[answer] => ok
)
[2] => Array
(
[datum] => 2019
)
[3] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 930
[cid] => 202
[answer] => ok
)
[4] => Array
(
[datum] => 2019
)
[5] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 928
[cid] => 202
[answer] => ok
)
[6] => Array
(
[datum] => 2019
)
[7] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 926
[cid] => 202
[answer] => ok
)
[8] => Array
(
[datum] => 2019
)
[9] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 924
[cid] => 202
[answer] => ok
)
[10] => Array
(
[datum] => 2019
)
[11] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 922
[cid] => 202
[answer] => ok
)
[12] => Array
(
[datum] => 2019
)
[13] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 920
[cid] => 202
[answer] => ok
)
[14] => Array
(
[datum] => 2019
)
[15] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 918
[cid] => 202
[answer] => fout
)
[16] => Array
(
[datum] => 2019
)
[17] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 916
[cid] => 202
[answer] => ok
)
[18] => Array
(
[datum] => 2020
)
[19] => Array
(
[infoid] => 236
[company_id] => 57
[datum] => 2020-03-21
[tid] => 324
[catid] => 230
[info_id] => 236
[questionid] => 1192
[cid] => 230
[answer] => ok
)
[20] => Array
(
[datum] => 2020
)
[21] => Array
(
[infoid] => 236
[company_id] => 57
[datum] => 2020-03-21
[tid] => 324
[catid] => 230
[info_id] => 236
[questionid] => 1191
[cid] => 230
[answer] => ok
)
[22] => Array
(
[datum] => 2020
)
[23] => Array
(
[infoid] => 236
[company_id] => 57
[datum] => 2020-03-21
[tid] => 324
[catid] => 230
[info_id] => 236
[questionid] => 1190
[cid] => 230
[answer] => fout
)
[24] => Array
(
[datum] => 2020
)
[25] => Array
(
[infoid] => 236
[company_id] => 57
[datum] => 2020-03-21
[tid] => 324
[catid] => 230
[info_id] => 236
[questionid] => 1189
[cid] => 230
[answer] => ok
)
[26] => Array
(
[datum] => 2019
)
[27] => Array
(
[infoid] => 235
[company_id] => 57
[datum] => 2019-04-12
[tid] => 329
[catid] => 229
[info_id] => 235
[questionid] => 1187
[cid] => 229
[answer] => ok
)
[28] => Array
(
[datum] => 2019
)
[29] => Array
(
[infoid] => 235
[company_id] => 57
[datum] => 2019-04-12
[tid] => 329
[catid] => 228
[info_id] => 235
[questionid] => 1186
[cid] => 228
[answer] => ok
)
[30] => Array
(
[datum] => 2019
)
[31] => Array
(
[infoid] => 235
[company_id] => 57
[datum] => 2019-04-12
[tid] => 329
[catid] => 228
[info_id] => 235
[questionid] => 1185
[cid] => 228
[answer] => ok
)
[32] => Array
(
[datum] => 2020
)
[33] => Array
(
[infoid] => 236
[company_id] => 57
[datum] => 2020-03-21
[tid] => 324
[catid] => 230
[info_id] => 236
[questionid] => 1188
[cid] => 230
[answer] => ok
)
[34] => Array
(
[datum] => 2019
)
[35] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 934
[cid] => 202
[answer] => ok
)
[36] => Array
(
[datum] => 2019
)
[37] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 936
[cid] => 202
[answer] => ok
)
[38] => Array
(
[datum] => 2019
)
[39] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 938
[cid] => 202
[answer] => ok
)
[40] => Array
(
[datum] => 2019
)
[41] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 940
[cid] => 202
[answer] => ok
)
[42] => Array
(
[datum] => 2019
)
[43] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 942
[cid] => 202
[answer] => ok
)
[44] => Array
(
[datum] => 2019
)
[45] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 944
[cid] => 202
[answer] => ok
)
[46] => Array
(
[datum] => 2019
)
[47] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 946
[cid] => 202
[answer] => ok
)
[48] => Array
(
[datum] => 2019
)
[49] => Array
(
[infoid] => 215
[company_id] => 57
[datum] => 2019-04-10
[tid] => 327
[catid] => 202
[info_id] => 215
[questionid] => 948
[cid] => 202
[answer] => ok
)
[50] => Array
(
[datum] => 2019
)
[51] => Array
(
[infoid] => 235
[company_id] => 57
[datum] => 2019-04-12
[tid] => 329
[catid] => 228
[info_id] => 235
[questionid] => 1184
[cid] => 228
[answer] => ok
)
[52] => Array
(
[datum] => 2019
)
[53] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1154
[cid] => 220
[answer] => ok
)
[54] => Array
(
[datum] => 2019
)
[55] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1153
[cid] => 220
[answer] => ok
)
[56] => Array
(
[datum] => 2019
)
[57] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1152
[cid] => 220
[answer] => ok
)
[58] => Array
(
[datum] => 2019
)
[59] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1151
[cid] => 220
[answer] => ok
)
[60] => Array
(
[datum] => 2019
)
[61] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1150
[cid] => 220
[answer] => ok
)
[62] => Array
(
[datum] => 2019
)
[63] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1149
[cid] => 220
[answer] => ok
)
[64] => Array
(
[datum] => 2019
)
[65] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1147
[cid] => 220
[answer] => ok
)
[66] => Array
(
[datum] => 2019
)
[67] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1148
[cid] => 220
[answer] => fout
)
[68] => Array
(
[datum] => 2019
)
[69] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1145
[cid] => 220
[answer] => ok
)
[70] => Array
(
[datum] => 2019
)
[71] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1146
[cid] => 220
[answer] => ok
)
[72] => Array
(
[datum] => 2019
)
[73] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1144
[cid] => 220
[answer] => ok
)
[74] => Array
(
[datum] => 2019
)
[75] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1143
[cid] => 220
[answer] => ok
)
[76] => Array
(
[datum] => 2019
)
[77] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1142
[cid] => 220
[answer] => ok
)
[78] => Array
(
[datum] => 2019
)
[79] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1140
[cid] => 220
[answer] => ok
)
[80] => Array
(
[datum] => 2019
)
[81] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1141
[cid] => 220
[answer] => ok
)
[82] => Array
(
[datum] => 2019
)
[83] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1139
[cid] => 220
[answer] => ok
)
[84] => Array
(
[datum] => 2019
)
[85] => Array
(
[infoid] => 230
[company_id] => 57
[datum] => 2019-04-20
[tid] => 327
[catid] => 220
[info_id] => 230
[questionid] => 1138
[cid] => 220
[answer] => ok
)
)
还有我的PHP代码:
<?PHP
while($getwpi = $getwpicon->fetch_assoc()){
$year = date('Y', strtotime($getwpi['datum']));
$wpi[]['datum'] = $year;
$wpi[] = $getwpi;
}
echo '<pre>';
print_r($wpi);
echo '</pre>';
?>
我也尝试过:
$wpi[]['datum'][] = $year;
但这仍然会添加一个新数组。
答案 0 :(得分:2)
请注意,$wpi[]['datum'] = $year;
是指将新元素添加到具有键数据的数组中,但您只想更新当前键。更新应该在$getwpi
上,因为这是您添加到结果数组中的元素。
您应该这样做:
while($getwpi = $getwpicon->fetch_assoc()){
$year = date('Y', strtotime($getwpi['datum']));
$getwpi['datum'] = $year; // update your field
$wpi[] = $getwpi; // add to the result array
}