2016-03-05 62 views
-6

這裏的例子數組:重新創建一個數組,這樣我得到的結果,因爲我想

$arr = array(array (
    'country' => 
    array (
    'code' => 'in', 
    'name' => 'India', 
), 
    'language' => 
    array (
    'code' => 'en', 
    'name' => 'English', 
), 
), 
array (
    'country' => 
    array (
    'code' => 'in', 
    'name' => 'India', 
), 
    'language' => 
    array (
    'code' => 'hi', 
    'name' => 'Hindi', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'id', 
    'name' => 'Indonesia', 
), 
    'language' => 
    array (
    'code' => 'id', 
    'name' => 'Indonesian', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'it', 
    'name' => 'Italy', 
), 
    'language' => 
    array (
    'code' => 'it', 
    'name' => 'Italian', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'jp', 
    'name' => 'Japan', 
), 
    'language' => 
    array (
    'code' => 'ja', 
    'name' => 'Japanese', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'mx', 
    'name' => 'Mexico', 
), 
    'language' => 
    array (
    'code' => 'es', 
    'name' => 'Spanish; Castilian', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'nl', 
    'name' => 'Netherlands', 
), 
    'language' => 
    array (
    'code' => 'nl', 
    'name' => 'Dutch', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'ng', 
    'name' => 'Nigeria', 
), 
    'language' => 
    array (
    'code' => 'en', 
    'name' => 'English', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'no', 
    'name' => 'Norway', 
), 
    'language' => 
    array (
    'code' => 'no', 
    'name' => 'Norwegian', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'pk', 
    'name' => 'Pakistan', 
), 
    'language' => 
    array (
    'code' => 'en', 
    'name' => 'English', 
), 
), 

array (
    'country' => 
    array (
    'code' => 'ph', 
    'name' => 'Philippines', 
), 
    'language' => 
    array (
    'code' => 'en', 
    'name' => 'English', 
), 
)); 

正如你可能已經注意到,在上述陣列印度被提及兩次,現在,我要的是,如果兩個或更多陣列包含相同的國家,那麼所有包含在同一個國家的數組成爲一個陣列是這樣的:

$arr = array (array (
    'country' => 
    array (
    'code' => 'in', 
    'name' => 'India', 
), 
    'language' => array(
    array (
    'code' => 'en', 
    'name' => 'English', 
), 
    array (
    'code' => 'hi', 
    'name' => 'Hindi', 
), 
) 
) 
); 

和陣列的其餘部分保持原樣。

+0

你舉的例子的情況是非常大的;你能提供一個你想要做的最簡單的例子嗎? – Vulcan

+0

我投票結束這個問題作爲題外話,因爲SO不是衆包代碼生成器。 –

+0

@ harry123我不能爲別人說話,但我低估了你的問題,因爲stackoverflow不是代碼編寫服務。 – Sculper

回答

0

像這樣的東西可能一開始工作:

<?php 
    $Data = array (
     array (
      'country' => array (
       'code' => 'in', 
       'name' => 'India' 
      ), 

      'language' => array (
       'code' => 'en', 
       'name' => 'English' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'in', 
       'name' => 'India' 
      ), 

      'language' => array (
       'code' => 'hi', 
       'name' => 'Hindu' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'id', 
       'name' => 'Indonesia' 
      ), 

      'language' => array (
       'code' => 'id', 
       'name' => 'Indonesian' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'it', 
       'name' => 'Italy', 
      ), 

      'language' => array (
       'code' => 'it', 
       'name' => 'Italian' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'jp', 
       'name' => 'Japan' 
      ), 

      'language' => array (
       'code' => 'ja', 
       'name' => 'Japanese' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'mx', 
       'name' => 'Mexico' 
      ), 

      'language' => array (
       'code' => 'es', 
       'name' => 'Spanish; Castilian' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'nl', 
       'name' => 'Netherlands' 
      ), 

      'language' => array (
       'code' => 'nl', 
       'name' => 'Dutch' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'ng', 
       'name' => 'Nigeria' 
      ), 

      'language' => array (
       'code' => 'en', 
       'name' => 'English' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'no', 
       'name' => 'Norway' 
      ), 

      'language' => array (
       'code' => 'no', 
       'name' => 'Norwegian' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'pk', 
       'name' => 'Pakistan' 
      ), 

      'language' => array (
       'code' => 'en', 
       'name' => 'English' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'ph', 
       'name' => 'Philippines' 
      ), 

      'language' => array (
       'code' => 'en', 
       'name' => 'English' 
      ) 
     ), 

     array (
      'country' => array (
       'code' => 'nl', 
       'name' => 'Netherlands' 
      ), 

      'language' => array (
       'code' => 'en', 
       'name' => 'English' 
      ) 
     ) 
    ); 

    function ProcessData ($Data) { 
     $Res = array(); 

     $m = array(); 
     foreach ($Data as $k => $v) { 
      $Country = $v ['country']; 

      if (in_array ($Country ['name'], $m)) { 
       continue; 
      } 

      $Lang = array(); 
      $Lang [] = $v ['language']; 

      for ($i = $k + 1, $l = count ($Data); $i < $l; $i++) { 
       if ($Data [$i] ['country'] ['name'] === $Country ['name']) { 
        $Lang [] = $Data [$i] ['language']; 
       } 
      } 

      $m [] = $Country ['name']; 

      $Res [] = array (
       'country' => $v ['country'] ['name'], 
       'language' => $Lang 
      ); 
     } 

     return $Res; 
    } 

    print_r (ProcessData ($Data)); 

?> 

,並提供:

Array 
(
    [0] => Array 
     (
      [country] => India 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => en 
          [name] => English 
         ) 

        [1] => Array 
         (
          [code] => hi 
          [name] => Hindu 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [country] => Indonesia 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => id 
          [name] => Indonesian 
         ) 

       ) 

     ) 

    [2] => Array 
     (
      [country] => Italy 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => it 
          [name] => Italian 
         ) 

       ) 

     ) 

    [3] => Array 
     (
      [country] => Japan 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => ja 
          [name] => Japanese 
         ) 

       ) 

     ) 

    [4] => Array 
     (
      [country] => Mexico 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => es 
          [name] => Spanish; Castilian 
         ) 

       ) 

     ) 

    [5] => Array 
     (
      [country] => Netherlands 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => nl 
          [name] => Dutch 
         ) 

        [1] => Array 
         (
          [code] => en 
          [name] => English 
         ) 

       ) 

     ) 

    [6] => Array 
     (
      [country] => Nigeria 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => en 
          [name] => English 
         ) 

       ) 

     ) 

    [7] => Array 
     (
      [country] => Norway 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => no 
          [name] => Norwegian 
         ) 

       ) 

     ) 

    [8] => Array 
     (
      [country] => Pakistan 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => en 
          [name] => English 
         ) 

       ) 

     ) 

    [9] => Array 
     (
      [country] => Philippines 
      [language] => Array 
       (
        [0] => Array 
         (
          [code] => en 
          [name] => English 
         ) 

       ) 

     ) 

) 
+0

謝謝!當我第一次嘗試你的代碼時,它不起作用,但現在當我重試時,我發現它確實有效,也許我做錯了什麼。再次感謝! – harry123

+0

樂意幫忙:) – Anders

0

我發現多了一個解決方案,但是@anders代碼也在努力!

$res = array(); 

foreach($arr as $val) 
{ 
    if(!isset($res[$val['country']['name']])) 

    $res[$val['country']['name']] = array('code'=>$val['country']['code'], 'name'=>$val['country']['name'], 'language'=>array()); 

    $res[$val['country']['name']]['language'][] = array('code'=>$val['language']['code'], 'name'=>$val['language']['name']); 
} 
echo "<pre>"; 
print_r(var_export(array_values($res))); 
echo "</pre>"; 

Here is a demo of working example.

相關問題