2017-02-09 56 views
0

是否有可能合并相同的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++; 
    } 
> 

我试图搜索这个问题。但我没有得到有关这个问题的答案。

请帮助我如何实现这个问题。

谢谢。

+1

可以请你发布你的数组? –

+0

我更新我的代码..谢谢。 – Joehamir

+0

从数据库中做这些值? – affaz

回答

1

下面是做到这一点的一种方法:

$in = [ 
    '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, 
]; 

$out = []; 
foreach ($in as $key => $value) { 
    $parts = explode('_', $key); 
    if (empty($out[$parts[0]])) { 
     $out[$parts[0]] = []; 
    } 
    $out[$parts[0]][$parts[1]] = $value; 
} 

echo '<table><thead> 
     <tr> 
     <th>Country</th> 
     <th>Jan 2017</th> 
     <th>Frb 2017</th> 
     </tr> 
     </thead><tbody>'; 
foreach ($out as $country => $data) { 
    $jan = isset($data['01-2017']) ? $data['01-2017'] : 0; 
    $feb = isset($data['02-2017']) ? $data['02-2017'] : 0; 
    echo "<tr> 
      <td>$country</td> 
      <td>$jan</td> 
      <td>$feb</td> 
      </tr>"; 
} 
echo '</tbody></table>'; 

这会工作,但它是做一个可怕的方式,你真的应该使用一些模板引擎如树枝生成HTML,所以你将计算与视图生成分开。

0

我认为你需要有一个地图(国家名​​称作为键的数组),并为每个国家保存一对值。喜欢的东西

$model = array('Japan' => array(4670, 1271), ...); 

然后你只需遍历当前模型,并构建新的一个:

foreach ($sums as $country => $value) { 
    if (! $model[$country]) { 
     $model[$country] = array(NULL, NULL); 
    } 
    $model[$country][$valueIsFromFirstMonth ? 0 : 1] = value; 
} 

然后你就可以循环通过新的$模型并进行打印。

相关问题