使用foreach循环php在另一个数组中创建数组
已经四个小时了,我在谷歌和软件上到处搜索,但找不到答案。这就是我迄今为止所尝试的。这是到目前为止我的代码使用foreach循环php在另一个数组中创建数组,php,Php,已经四个小时了,我在谷歌和软件上到处搜索,但找不到答案。这就是我迄今为止所尝试的。这是到目前为止我的代码 $tmp_array = array(); foreach ($cms as $key => $val) { $cDate = date('Ym', strtotime($val['day_date'])); $tmp_ids[] = $val['id']; if (array_
$tmp_array = array();
foreach ($cms as $key => $val) {
$cDate = date('Ym', strtotime($val['day_date']));
$tmp_ids[] = $val['id'];
if (array_key_exists($cDate, $tmp_array)) {
$tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
$tmp_array[$cDate]['ids'] = $tmp_ids;
} else {
$tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
$tmp_array[$cDate]['ids'] = $tmp_ids;
}
}
它的产量是这样的,
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 31
[1] => 32
[2] => 33
[3] => 34
)
)
)
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 33
[1] => 34
)
)
)
但是我想要这样的结果数组
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 31
[1] => 32
[2] => 33
[3] => 34
)
)
)
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 33
[1] => 34
)
)
)
任何建议,我在代码中做什么
ID是表的主键,“新访问者”是访问我的站点的访问者的计数。但到目前为止,我还没有找到任何解决办法
这是我的$cms阵列
Array
(
[0] => Array
(
[id] => 31
[day_date] => 2020-01-30 00:00:00
[new_visitors] => 459
)
[1] => Array
(
[id] => 32
[day_date] => 2020-01-31 00:00:00
[new_visitors] => 338
)
[2] => Array
(
[id] => 33
[day_date] => 2020-02-01 00:00:00
[new_visitors] => 242
)
[3] => Array
(
[id] => 34
[day_date] => 2020-02-02 00:00:00
[new_visitors] => 219
)
)
谢谢你,米索鲁德,你的评论奏效了
$tmp_array = array();
foreach ($cms as $key => $val) {
$cDate = date('Ym', strtotime($val['day_date']));
if (array_key_exists($cDate, $tmp_array)) {
$tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
$tmp_array[$cDate]['ids'][] = $val['id'];
} else {
$tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
$tmp_array[$cDate]['ids'][] = $val['id'];
}
}
$arr=[
[id'=>31、'day\u date'=>2020-01-30 00:00:00、'new\u visitors'=>459],
[id'=>32,'day\u date'=>2020-01-31 00:00:00,'new\u visitors'=>338],
[id'=>33、'day\u date'=>2020-02-01 00:00:00、'new\u visitors'=>242],
['id'=>34,'day\u date'=>2020-02-02 00:00:00,'new\u visitors'=>219]
];
回声“;
印刷费($arr);
$data=[];
$add_visitor=0;
$day_date='';
foreach($arr作为$arr_值){
如果(日期('Ym',标准时间($arr_值['day_日期]))!=$day_日期){
$add_visitor=0;
}
$add_visitor+=$arr_value['new_visitor'];
$data[date('Ym',strotime($arr_value['day_date'])]['new_visitors']=$add_visitor;
$data[date('Ym',strotime($arr_value['day_date'])]['ids'][=$arr_value['id']);
$day_date=date('Ym',strotime($arr_value['day_date']);
}
打印(数据);
您没有在任何地方重置$tmp\u ID
,因此所有的ID当然都堆积在那里。完全移除$tmp\u ID
,只需以适当的方式将当前ID添加到数组中:$tmp\u数组[$cDate]['ID'[=$val['ID']代码>您可以进一步简化代码。查看我的答案更新。费萨尔·汗您有责任检查所有答案,然后在其中选出最佳答案。这样将来的访问者可以从你的帖子中得到帮助。