PHP按3个字母的国家代码对数组进行分组/排序


PHP group/sort an array by 3 letter country code

我有这样一个数组:

  [1]=>
  array(4) {
    ["id"]=>
    string(2) "31"
    ["slug"]=>
    string(11) "montpellier"
    ["title"]=>
    string(11) "Montpellier"
    ["country"]=>
    string(3) "fra"
  }
  [2]=>
  array(4) {
    ["id"]=>
    string(2) "30"
    ["slug"]=>
    string(4) "york"
    ["title"]=>
    string(4) "York"
    ["country"]=>
    string(3) "gbr"
  }
  [3]=>
  array(4) {
    ["id"]=>
    string(2) "29"
    ["slug"]=>
    string(4) "hull"
    ["title"]=>
    string(4) "Hull"
    ["country"]=>
    string(3) "gbr"

和另一个数组:

$new_country = array(
    'gbr' => 'Great Britain',
    'fra' => 'France',
    'ita' => 'Italy',
    'de' => 'Germany',
    'esp' => 'Spain'    
);

我应该在数组上运行什么确切的函数来运行它通过$new_country数组并产生以下输出?

我想产生这样的输出:

<h2>France</h2>
<p>Montpellier</h2>
<h2>Great Britain</h2>
<p>York</p>
<p>Hull</p>

编辑

迄今为止最好的答案产生了这样的输出(国家是重复的):

Great Britain
Durham
France
Montpellier
Great Britain
York
Hull
Bradford
Leeds
Germany
Berlin
Great Britain
Leicester
Colchester
Oxford
Nottingham
Newcastle
St. Andrews
Loughborough
Chester
Ipswich
Bangor
Wolverhampton
Liverpool
Italy
Rome
Great Britain
Dundee
Sheffield
Bristol
Birmingham
Spain
Madrid
Barcelona
France
Paris
Great Britain
London
Manchester
Edinburgh
Italy
Turin
Great Britain
Glasgow

按国家排序

 usort($array,function($a,$b){
     return strcmp($a['country'],$b['country']);
 });
然后

 $lastc="";
 foreach($array as $v){
      if($v['country']!=$lastc){
           $lastc=$v['country'];
           print "<h2>$new_country[$lastc]</h2>";
      }
      print "<p>".$v['title'].'<p>';
 }

使用分组的其他解决方案

 $newarr=array();
 foreach($array as $v){
     $newarr[$v['country']][]=$v;
 }
 foreach($new_country as $k=>$v){
     if(isset($newarr[$k])){
          print '<h2>'.$v.'</h2>';
          foreach($newarr[$k] as $town)
             print '<p>'.$town['title'].'</p>';
     }
 }