用另一个多维arrray中的值替换多维数组值

时间:2017-01-16 04:22:07

标签: php arrays laravel multidimensional-array

长话短说,我有两个阵列:

$players - 此数组通过回合

将玩家名称与其ID匹配
array (
  1 => 
  array (
    '[GPS]Alt_Stab' => '1',
    '[SUC]Caution' => '43',
    '[SUC]Ralphis' => '42',
    '[gps]dranzer' => '23',
    '[gps]xen' => '123',
    '[suc]tony' => '34',
  ),
  2 => 
  array (
    '[SXP]Dragon' => '54',
    '[SXP]HumanBones' => '55',
    '[SXP]Torvald' => '62',
    '[TDT]DS' => '90',
    '[TDT]Dev' => '71',
    '[TDT]Heks' => '81',
  ),
  3 => 
  array (
    '[SXP]Dragon' => '39',
    '[SXP]HumanBones' => '55',
    '[TDT]DS' => '56',
    '[TDT]Dev' => '57',
    '[TDT]Heks' => '84',
    '[sxp]legion' => '65',
  ),
)

$events - 通过回合

保存事件数据
array (
  0 => 
  array (
    1 => '5,,[SUC]Ralphis,18097663,0,0,0,1276,31,-16,20,0,13',
    2 => '15,[SUC]Ralphis,[GPS]Alt_Stab,18097667,1281,-54,-16,1317,-320,0,853016576,8,0',
    3 => '0,[SUC]Ralphis,[GPS]Alt_Stab,18097667,1281,-54,-16,1317,-320,0,45,0,11',
    4 => '15,[SUC]Caution,[gps]dranzer,18097674,913,381,-6,910,149,-8,802684928,8,0',
    5 => '0,[SUC]Caution,[gps]dranzer,18097674,913,381,-6,910,149,-8,100,0,11',
    etc.....),
  1 => 
  array (
    1 => '14,[TDT]Dev,,33266052,0,0,0,0,0,0,3,0,0',
    2 => '14,[TDT]DS,,33266150,0,0,0,0,0,0,2,0,0',
    3 => '14,[SXP]Dragon,,33266194,0,0,0,0,0,0,2,0,0',
    4 => '14,[SXP]Torvald,,33266212,0,0,0,0,0,0,7,1,0',
    5 => '14,[SXP]Torvald,,33266216,0,0,0,0,0,0,7,1,0',
    etc.....),
  2 => 
  array (
    1 => '14,[SXP]Dragon,,33287616,0,0,0,0,0,0,3,0,0',
    2 => '14,[TDT]DS,,33287659,0,0,0,0,0,0,3,0,0',
    3 => '15,[TDT]Heks,[SXP]HumanBones,33287691,2150,63,0,2316,295,0,207093760,8,0',
    4 => '15,[SXP]HumanBones,[TDT]Heks,33287691,2313,257,0,2090,64,0,661389312,8,0',
    5 => '0,[SXP]HumanBones,[TDT]Heks,33287691,2313,257,0,2090,64,0,100,0,11',

然后我设置了一个循环设置,它将遍历$players中的每个项目,并给我玩家的名字和他们的身份

for($i = 1; $i <= $round_count; $i++) {
    foreach($players[$i] as $player_name=>$player_id) {
        /* do things here */
    }
}

我需要以某种方式搜索$events[$round][$line]$player_name的事件字符串,并将其替换为$player_id,然后将结果推送到新的数组$r_events。我希望保持与$events相同的结构。

作为备注,$players$events将始终具有相同数量的最外层密钥。

$round_count将始终等于两个数组中最外层键的数量。

$round_count保留实际播放的轮数,因此上述for循环中的$event[$i-1]应包含第1轮的所有事件数据。

TLDR:我需要搜索$events[$round][$value] $players[$round][$key]并将其替换为$players[$round][$value]

预期结果:

$r_events

array:3 [▼
  0 => array:2085 [▼
    1 => "5,,PLAYERID,18097663,0,0,0,1276,31,-16,20,0,13"
    2 => "15,PLAYERID,PLAYERID,18097667,1281,-54,-16,1317,-320,0,853016576,8,0"
    3 => "0,PLAYERID,PLAYERID,18097667,1281,-54,-16,1317,-320,0,45,0,11"
    4 => "15,PLAYERID,PLAYERID,18097674,913,381,-6,910,149,-8,802684928,8,0"
    5 => "0,PLAYERID,PLAYERID,18097674,913,381,-6,910,149,-8,100,0,11"

0 个答案:

没有答案