2014-09-21 184 views
1

我有一個數組與所有聯繫人,我有一個數組與所有組。 如何將http://www.google.com/m8/feeds/groups/j**/base/3166d58a8c**25替換爲聯繫人數組中的組名YouTube? 由於Google Contacts API v3確實將組名稱存儲爲ID,因此我想用該組的名稱替換此ID。2個陣列之間的關係

E.g.

陣列組:

array(2) { 
    [0]=> 
    array(3) { 
    ["value"]=> 
    string(87) "http://www.google.com/m8/feeds/groups/**/base/***b87ad92" 
    ["name"]=> 
    string(4) "Family" 
    ["label"]=> 
    string(4) "Family" 
    } 
    [1]=> 
    array(3) { 
    ["value"]=> 
    string(87) "http://www.google.com/m8/feeds/groups/j**/base/3166d58a8c**25" 
    ["name"]=> 
    string(7) "YouTube" 
    ["label"]=> 
    string(7) "YouTube" 
    } 
} 

陣列聯繫人:

array(250) { 
    [0]=> 
    array(8) { 
    ["value"]=> 
    string(15) "X Y" 
    ["name"]=> 
    string(15) "X Y" 
    ["email"]=> 
    string(25) "[email protected]" 
    ["phone"]=> 
    string(10) "044564" 
    ["group"]=> 
    string(87) "http://www.google.com/m8/feeds/groups/j**/base/3166d58a8c**25" 
    ["phonework"]=> 
    string(10) "0479//804" 
    ["address"]=> 
    string(28) "55222 City" 
    ["label"]=> 
    string(15) "Bla" 
     } 
    ...... 
    } 

最後的結果將是:

array(250) { 
    [0]=> 
    array(8) { 
    ["value"]=> 
    string(15) "X Y" 
    ["name"]=> 
    string(15) "X Y" 
    ["email"]=> 
    string(25) "[email protected]" 
    ["phone"]=> 
    string(10) "044564" 
    ["group"]=> 
    string(87) "YouTube" 
    ["phonework"]=> 
    string(10) "0479//804" 
    ["address"]=> 
    string(28) "55222 City" 
    ["label"]=> 
    string(15) "Bla" 
     } 
    ...... 
    } 
+0

所以最後的輸出是什麼? – Ghost 2014-09-21 09:06:16

+0

我在問題中添加了所需的輸出。 – user3253002 2014-09-21 09:09:42

回答

0

只需創建一個嵌套循環。首先循環聯繫人(將被覆蓋),然後再將聯繫人分組。在內部循環,只是做一個平等的比較:

foreach($contacts as &$contact) { 
        //^reference 
    foreach($groups as $group) { 
     if($contact['group'] == $group['value']) { 
      $contact['group'] = $group['name']; 
     } 
    } 
}