是否有可能合并相同的foreach标签但不是值?pgp foreach合并相同的标签,但不是值
这是我的。
COUNTRY JAN 2017 FEB 2017
Japan US$4,670
Philippines US$4,589
United States US$2,001
Philippines US$1,462
Japan US$1,271
Australia US$1,096
Mexico US$998
United States US$725
United Arab Emirates US$655
Australia US$471
United Arab Emirates US$337
Singapore US$316
Switzerland US$240
Singapore US$201
United Kingdom US$158
Germany US$140
Netherlands US$140
Thailand US$126
New Zealand US$120
Taiwan US$70
Sweden US$70
Austria US$65
这里是我想要显示
COUNTRY JAN 2017 FEB 2017
Japan US$4,670 US$1,271
Philippines US$4,589 US$1,462
United States US$2,001 US$725
Australia US$1,096 US$471
Mexico US$998
United Arab Emirates US$337 US$655
Singapore US$316 US$201
Switzerland US$240
United Kingdom US$158
Germany US$140
Netherlands US$140
Thailand US$126
New Zealand US$120
Taiwan US$70
Sweden US$70
Austria US$65
这里是输出
Array
(
[Japan_01-2017] => 4670
[Philippines_01-2017] => 4589
[United States_01-2017] => 2001
[Philippines_02-2017] => 1462
[Japan_02-2017] => 1271
[Australia_01-2017] => 1096
[Mexico_01-2017] => 998
[United States_02-2017] => 725
[United Arab Emirates_02-2017] => 655
[Australia_02-2017] => 471
[United Arab Emirates_01-2017] => 337
[Singapore_01-2017] => 316
[Switzerland_01-2017] => 240
[Singapore_02-2017] => 201
[United Kingdom_01-2017] => 158
[Germany_01-2017] => 140
[Netherlands_01-2017] => 140
[Thailand_01-2017] => 126
[New Zealand_02-2017] => 120
[Taiwan_01-2017] => 70
[Sweden_01-2017] => 70
[Austria_01-2017] => 65
)
这里是我的代码。
$countryarr[$i][$data['delivery_country']][$data['month'].'-'.$data['year']] = $usdval;
$i++;
}
$sums = array();
foreach ($countryarr as $key => $values) {
foreach ($values as $label => $count) {
foreach ($count as $label2 => $count2) {
$cmy = $label.'_'.$label2;
/* echo $label.' '.$label2.' '.$count2.'<br/>'; */
if (!array_key_exists($cmy, $sums)) {
$sums[$cmy] = 0;
}
$sums[$cmy] += $count2;
}
}
}
arsort($sums);
foreach ($sums as $nlabel => $ncount) {
$cex = explode('_',$nlabel);
if($cex[1]==$last_monthn){
$lastmonthsales = $currencies->format(($ncount/$currencies->get_value(strtoupper('USD'))), true, 'USD');
}else{
$lastmonthsales = "";
}
if($cex[1]==$this_monthn){
$thismonthsales = $currencies->format(($ncount/$currencies->get_value(strtoupper('USD'))), true, 'USD');
}else{
$thismonthsales = "";
}
?>
<tr>
<td><?php echo $cex[0]; ?></td>
<td><?php echo $lastmonthsales; ?></td>
<td><?php echo $thismonthsales ?></td>
<td></td>
<td></td>
</tr>
<?php
$a++;
}
>
我试图搜索这个问题。但我没有得到有关这个问题的答案。
请帮助我如何实现这个问题。
谢谢。
可以请你发布你的数组? –
我更新我的代码..谢谢。 – Joehamir
从数据库中做这些值? – affaz