2014-09-21 132 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']; 
     } 
    } 
}