2016-07-12 16 views
0

私はこの配列を持っていますが、私の条件で並べ替えたいです。PHP配列基準で並べ替え

「国」と「トップ」が同じ場合は、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 = 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 
     (
      [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 
     ) 
) 

基準(一致している必要があります): 国 トップ と、この配列を持っているどのようにトップカウント(合計)

私は非常にgratef ul誰かがこの問題をどう扱うか考えているならば。前もって感謝します。

+0

あなたの予想される出力が間違っているかもしれない、または非常に不明なロジックです。 – bansi

+0

「foreach」で始まる –

+0

@u_mulder大丈夫ですが、それでは。あなたはそれを作る方法を知っていましたか? – diank

答えて

1

次のコードを試してください。結果にはまったくの変更があります。しかし、私はそれがあなたのために働くと信じています

$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); 
関連する問題