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,但我还没有想出任何工作的解决方案。这也是我从第三方接收的所有数据,所以我无法控制初始数组设置。

谢谢你的帮助和建议!

实际上,这很简单。(我不认为有一个内置函数可以做到这一点。)

循环$arr2$arr1下面的循环。在循环中,只需添加一个条件,如果两个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
                )
        )
)

遍历$arr2,并从匹配的$arr1数组值中添加数据:

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

未测试此代码

如果我理解正确,您希望为$arr2['games']数组中的每个数组添加name索引。

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

相关内容

  • 没有找到相关文章

最新更新