2016-07-12 101 views
0

我有這個數組,但我想按照我的標準對其進行排序。PHP Array按標準排序

我想如果「國家」和「頂部」是相同的,那麼他們希望在一個地方。稍微低於我的意思的例子。

$array = array(
    array("city" => "New York", "country" => "USA", "top" => "1"), 
    array("city" => "London", "country" => "UK", "top" => "1"), 
    array("city" => "Sofia", "country" => "BG", "top" => "1"), 
    array("city" => "Belgrad", "country" => "SRB", "top" => "2"), 
    array("city" => "Varna", "country" => "BG", "top" => "2"), 
    array("city" => "LA", "country" => "UK", "top" => "1"), 
    array("city" => "Bat", "country" => "USA", "top" => "1") 
); 

這裏是數組:

Array 
(
    [0] => Array 
     (
      [city] => New York 
      [country] => USA 
      [top] => 1 
     ) 

    [1] => Array 
     (
      [city] => London 
      [country] => UK 
      [top] => 1 
     ) 

    [2] => Array 
     (
      [city] => Sofia 
      [country] => BG 
      [top] => 1 
     ) 

    [3] => Array 
     (
      [city] => Belgrad 
      [country] => SRB 
      [top] => 2 
     ) 

    [4] => Array 
     (
      [city] => Varna 
      [country] => BG 
      [top] => 2 
     ) 

    [5] => Array 
     (
      [city] => LA 
      [country] => USA 
      [top] => 1 
     ) 

    [6] => Array 
     (
      [city] => Bat 
      [country] => UK 
      [top] => 1 
     ) 
) 

我想這樣的結果:

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [city] => New York 
        [country] => USA 
        [top] => 1 
       ) 

      [1] => Array 
       (
        [city] => LA 
        [country] => USA 
        [top] => 1 
       ) 
      [2] => Array 
       (
        [top_cities] => 2 
       ) 
     ) 

    [1] => Array 
     (
      [0] => Array 
       (
        [city] => London 
        [country] => UK 
        [top] => 1 
       ) 
      [1] => Array 
       (
        [city] => Bat 
        [country] => UK 
        [top] => 1 
       ) 
      [2] => Array 
       (
        [top_cities] => 2 
       ) 
     ) 

    [2] => Array 
     (
      [0] => Array 
       (
        [city] => Sofia 
        [country] => BG 
        [top] => 1 
       ) 
     ) 

    [3] => Array 
     (
      [city] => Belgrad 
      [country] => SRB 
      [top] => 2 
     ) 

    [4] => Array 
     (
      [city] => Varna 
      [country] => BG 
      [top] => 2 
     ) 
) 

標準(必須匹配): 國家 頂部 和怎麼算頂部有這個數組中(總和)

我會很格拉夫ul如果有人提出一個想法如何處理這個問題。提前致謝。

+0

可能是你的預期輸出是錯誤的,或者很不清楚的邏輯。 – bansi

+0

從'foreach'開始 –

+0

@u_mulder好的,但後來。你有沒有想法如何做到這一點? – diank

回答

1

請嘗試下面的代碼。結果有amall修改。但我相信它會適用於你

$array = array(
    array("city" => "New York", "country" => "USA", "top" => "1"), 
    array("city" => "London", "country" => "UK", "top" => "1"), 
    array("city" => "Sofia", "country" => "BG", "top" => "1"), 
    array("city" => "Belgrad", "country" => "SRB", "top" => "2"), 
    array("city" => "Varna", "country" => "BG", "top" => "2"), 
    array("city" => "LA", "country" => "UK", "top" => "1"), 
    array("city" => "Bat", "country" => "USA", "top" => "1") 
); 

$newArray = array(); 
foreach($array as $key => $val){ 
    $newArray[$val['country']][] = $val; 
} 
print_r($newArray);