按日期和其他标识符拆分数组



我有从mongodb到数组的数据,我想处理返回数据,按日期和其他值进行分割,这就是我目前所得到的,我需要一些关于结果的帮助

<?php
$stats=array(
array("source"=>501,"status"=>"answered","dest"=>"100","date"=>"2022-17-01"),
array("source"=>501,"status"=>"noAnswer","dest"=>"120","date"=>"2022-17-01"),
array("source"=>501,"status"=>"answered","dest"=>"100","date"=>"2022-18-01"),
array("source"=>502,"status"=>"answered","dest"=>"120","date"=>"2022-17-01"),
array("source"=>502,"status"=>"answered","dest"=>"130","date"=>"2022-17-01"),
array("source"=>502,"status"=>"answered","dest"=>"110","date"=>"2022-18-01")
);
$new1 = array();
$answer = 0;
$noanswer = 0;
$lastsrc = '';
$lastdate = '';
$dest=0;
foreach ($stats as $new) {
$src = $new['source'];
$date = $new['date'];

if ( $lastsrc == $src && $lastdate == $date ) {
$dest++;
if($new['status'] == 'answered'){ $answer++; }
if($new['status'] == 'noanswer'){ $noanswer++;}
$new1[] = array('source' => $src, 
'date' => $date, 
'ans' => $answer, 
'nonans' => $noanswer, 
'dest' => $dest  );
} else if ( $lastsrc == $src && $lastdate != $date ) {
$dest++;
if($new['status'] == 'answered'){ $answer++;}
if($new['status'] == 'noanswer'){ $noanswer++;}
$new1[] = array('source' => $src, 
'date' => $date, 
'ans' => $answer, 
'nonans' => $noanswer, 
'dest' => $dest  );
} else {
$dest++;
if($new['status'] == 'answered'){ $answer++; }
if($new['status'] == 'noanswer'){ $noanswer++;}

$new1[] = array('source' => $src, 
'date' => $date, 
'ans' => $answer,
'nonans' => $noanswer, 
'dest' => $dest  );
$lastsrc = $src;
$lastdate = $date;
}
}   
print_r($new1);
?>

我试图实现的是按日期和来源分割数据,所以如果在foreach循环中我发现相同的来源和日期,那么我修改数组而不是创建新数组,如果相反,那么创建新数组

我试图得到的结果是:

array("source"=>501,"ans"=>"1","noans" => 0,"dest"=>1,"date"=>"2022-17-01"),
array("source"=>501,"ans"=>"1","noAnswer" => 1,"dest"=>2,"date"=>"2022-18-01"),
array("source"=>502,"ans"=>"2","noans" => 0,"dest"=>2,"date"=>"2022-17-01"),
array("source"=>502,"ans"=>"1","noans" => 0,"dest"=>1,"date"=>"2022-18-01")

我不确定这是否是正确的方式,任何帮助都将非常感谢

<?php
$stats = [
[ 'source' => 501, 'status' => 'answered', 'dest' => '100', 'date' => '2022-17-01' ],
[ 'source' => 501, 'status' => 'noAnswer', 'dest' => '120', 'date' => '2022-17-01' ],
[ 'source' => 501, 'status' => 'answered', 'dest' => '100', 'date' => '2022-18-01' ],
[ 'source' => 502, 'status' => 'answered', 'dest' => '120', 'date' => '2022-17-01' ],
[ 'source' => 502, 'status' => 'answered', 'dest' => '130', 'date' => '2022-17-01' ],
[ 'source' => 502, 'status' => 'answered', 'dest' => '110', 'date' => '2022-18-01' ]
];
$result = [];
foreach ($stats as $stat) {
$source = $stat['source'];
$date = $stat['date'];
$closure = static fn($item) => $item['source'] === $source && $item['date'] === $date;
$found = array_filter($result, $closure);
if (count($found) === 0) {
$list = array_filter($stats, $closure);
$list = array_map(static fn($item) => [ 'status' => $item['status'], 'dest' => $item['dest'] ], $list);
$result[] = [
'source' => $source,
'date' => $date,
'answered' => count(array_filter(array_column($list, 'status'), static fn($item) => $item === 'answered')),
'noAnswer' => count(array_filter(array_column($list, 'status'), static fn($item) => $item === 'noAnswer')),
'dest' => count(array_column($list, 'dest')),
];
}
}
print_r($result);

从要排序的字段中创建一个复合键,并使用关联数组。(数组中的单个级别使排序/合并以及随后提取值(array_values()(变得容易(然后您只需要在$stats$result上分别迭代一次。

$result = [];
foreach($stats as $stat) {
$key = $stat['source'].'_'.$stat['date'];
if (!array_key_exists($key, $result)) {
$result[$key] = ['answered' => 0, 'noAnswer' => 0] + $stat;
}
$result[$key][$stat['status']]++;
$result[$key]['dest'][$stat['dest']] = 1;
}
$result = array_map( // Transform "destinations key store" to count
function($item) { 
$item['dest'] = count($item['dest']); return $item; 
},
$result
);
print_r(array_values($result));
['source' => 501, 'status' => answered, 'dest' => 2, 'date' => 2022-17-01, 'answered' => 1, 'noAnswer' => 1],
['source' => 501, 'status' => answered, 'dest' => 1, 'date' => 2022-18-01, 'answered' => 1, 'noAnswer' => 0],
['source' => 502, 'status' => answered, 'dest' => 2, 'date' => 2022-17-01, 'answered' => 2, 'noAnswer' => 0],
['source' => 502, 'status' => answered, 'dest' => 1, 'date' => 2022-18-01, 'answered' => 1, 'noAnswer' => 0]

相关内容

  • 没有找到相关文章

最新更新