2014-07-01 140 views
3

我有,我有工作2個多維數組:PHP替換數組值

$arr1 = 
Array 
([type] => characters 
[version] => 5.6.7.8 
[data] => Array 
      ([Char1] => Array 
        ([id] => 1 
        [name] =>Char1 
        [title] =>Example 
        [tags] => Array 
          ([0] => DPS 
          [1] => Support)) 
      [Char2] => Array 
        ([id] => 2 
        [name] =>Char2 
        [title] =>Example 
        [tags] => Array 
          ([0] => Tank 
          [1] => N/A) 
        ) 
      ) 

等等

$arr2= 
Array 
([games] => Array 
     ([gameId] => 123 
      [gameType => Match 
      [char_id] => 1 
      [stats] => Array 
        ([damage] => 55555 
        [kills] => 5) 
     ) 
     ([gameId] => 157 
      [gameType => Match 
      [char_id] => 2 
      [stats] => Array 
        ([damage] => 12642 
        [kills] => 9) 
     ) 

等等

基本上,我需要的幾乎所有$arr2中的數據...但只有來自$arr1的Char名稱。我怎樣才能合併或將$arr1['name'] key=>value合併到$arr2中,其中$arr1['id']等於$arr2['char_id'],因爲每個數組的「id」字段是相同的數字。

我試過使用array_mergearray_replace,但我還沒有拿出任何工作解決方案。這也是我從第三方接收的所有數據,所以我無法控制初始陣列設置。

感謝您的任何幫助或建議!

+0

只需使用一個'foreach'遍歷'$ arr2' ,並且爲每個元素遍歷'$ arr1'來獲取你想要的元素。 –

回答

1

其實這很直截了當。 (我不認爲有內置功能可以做到這一點。)

迴路$arr2及其下的迴路也$arr1。 while循環中,只需添加一個條件,如果兩個ID匹配,則將該特定名稱添加到$arr2。 (和使用上$arr2一些參考&

考慮這個例子:

// your data 
$arr1 = array(
    'type' => 'characters', 
    'version' => '5.6.7.8', 
    'data' => array(
     'Char1' => array(
      'id' => 1, 
      'name' => 'Char1', 
      'title' => 'Example', 
      'tags' => array('DPS', 'Support'), 
     ), 
     'Char2' => array(
      'id' => 2, 
      'name' => 'Char2', 
      'title' => 'Example', 
      'tags' => array('Tank', 'N/A'), 
     ), 

    ), 
); 

$arr2 = array(
    'games' => array(
     array(
      'gameId' => 123, 
      'gameType' => 'Match', 
      'char_id' => 1, 
      'stats' => array('damage' => 55555, 'kills' => 5), 
     ), 
     array(
      'gameId' => 157, 
      'gameType' => 'Match', 
      'char_id' => 2, 
      'stats' => array('damage' => 12642, 'kills' => 9), 
     ), 
    ), 
); 

foreach($arr2['games'] as &$value) { 
    $arr2_char_id = $value['char_id']; 
    // loop and check against the $arr1 
    foreach($arr1['data'] as $element) { 
     if($arr2_char_id == $element['id']) { 
      $value['name'] = $element['name']; 
     } 
    } 
} 

echo '<pre>'; 
print_r($arr2); 

$arr2現在應該是這樣的:

Array 
(
    [games] => Array 
     (
      [0] => Array 
       (
        [gameId] => 123 
        [gameType] => Match 
        [char_id] => 1 
        [stats] => Array 
         (
          [damage] => 55555 
          [kills] => 5 
         ) 

        [name] => Char1 // <-- name 
       ) 

      [1] => Array 
       (
        [gameId] => 157 
        [gameType] => Match 
        [char_id] => 2 
        [stats] => Array 
         (
          [damage] => 12642 
          [kills] => 9 
         ) 

        [name] => Char2 // <-- name 
       ) 

     ) 
) 
+0

這正是我想要做的。我是編程新手,發現將我的頭圍繞嵌套循環,特別是foreach循環,有點困難。謝謝! – user3642863

0

遍歷$arr2和將數據從匹配$arr1數組值添加到它:

$i = 0; 
foreach($arr2['games'] as $arr2Game){ 
     $id = $arr2Game['char_id']; 
     $arr2['games'][$i]['name'] = $arr1['data'][$id]['name']; 
     $i++; 
} 

沒有測試此代碼。

0

如果我正確地理解了你,你想爲$arr2['games']數組中的每個數組添加一個name索引。

foreach($arr2['games'] as $key => $innerArray) 
{ 
    $arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name']; 
}