PHP:使用相同的值合并两个对象数组

时间:2014-11-12 13:52:48

标签: php arrays object array-merge

我得到了这两件事:

数组1

array(2) {
  [0]=>
  object(stdClass)#2 (6) {
    ["SteamId"]=>
    string(17) "76561197961360219"
    ["CommunityBanned"]=>
    bool(false)
    ["VACBanned"]=>
    bool(false)
    ["NumberOfVACBans"]=>
    int(0)
    ["DaysSinceLastBan"]=>
    int(0)
    ["EconomyBan"]=>
    string(4) "none"
  }
  [1]=>
  object(stdClass)#3 (6) {
    ["SteamId"]=>
    string(17) "76561198156224606"
    ["CommunityBanned"]=>
    bool(false)
    ["VACBanned"]=>
    bool(true)
    ["NumberOfVACBans"]=>
    int(1)
    ["DaysSinceLastBan"]=>
    int(1)
    ["EconomyBan"]=>
    string(4) "none"
  }
}

ARRAY2

array(2) {
  [0]=>
  object(stdClass)#5 (14) {
    ["steamid"]=>
    string(17) "76561198156224606"
    ["communityvisibilitystate"]=>
    int(3)
    ["profilestate"]=>
    int(1)
    ["personaname"]=>
    string(5) "Orion"
    ["lastlogoff"]=>
    int(1415641010)
    ["commentpermission"]=>
    int(1)
    ["profileurl"]=>
    string(39) "http://steamcommunity.com/id/sv4rtberg/"
    ["avatar"]=>
    string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/44/44780a8cb827a212dadbd0981c8834a2e1b4b7c7.jpg"
    ["avatarmedium"]=>
    string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/44/44780a8cb827a212dadbd0981c8834a2e1b4b7c7_medium.jpg"
    ["avatarfull"]=>
    string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/44/44780a8cb827a212dadbd0981c8834a2e1b4b7c7_full.jpg"
    ["personastate"]=>
    int(0)
    ["primaryclanid"]=>
    string(18) "103582791429521408"
    ["timecreated"]=>
    int(1411813740)
    ["personastateflags"]=>
    int(0)
  }
  [1]=>
  object(stdClass)#6 (11) {
    ["steamid"]=>
    string(17) "76561197961360219"
    ["communityvisibilitystate"]=>
    int(1)
    ["profilestate"]=>
    int(1)
    ["personaname"]=>
    string(9) "SVARTBERG"
    ["lastlogoff"]=>
    int(1415744339)
    ["commentpermission"]=>
    int(2)
    ["profileurl"]=>
    string(53) "http://steamcommunity.com/profiles/76561197961360219/"
    ["avatar"]=>
    string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/3e/3e649cc102a9d866272394ba321a123cb5b46c34.jpg"
    ["avatarmedium"]=>
    string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/3e/3e649cc102a9d866272394ba321a123cb5b46c34_medium.jpg"
    ["avatarfull"]=>
    string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/3e/3e649cc102a9d866272394ba321a123cb5b46c34_full.jpg"
    ["personastate"]=>
    int(0)
  }
}

如您所见,两个数组都包含保存一个相同值的对象(SteamId / steamid)。不,我想合并这些对象数组,以便我得到这样的东西:

array(2) {
  [0]=>
  object(stdClass)#5 (14) {
    ["steamid"]=>
    string(17) "76561198156224606"
    ["communityvisibilitystate"]=>
    int(3)
    ["profilestate"]=>
    int(1)
    ["personaname"]=>
    string(5) "Orion"
    ["lastlogoff"]=>
    int(1415641010)
    ["commentpermission"]=>
    int(1)
    ["profileurl"]=>
    string(39) "http://steamcommunity.com/id/sv4rtberg/"
    ["avatar"]=>
    string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/44/44780a8cb827a212dadbd0981c8834a2e1b4b7c7.jpg"
    ["avatarmedium"]=>
    string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/44/44780a8cb827a212dadbd0981c8834a2e1b4b7c7_medium.jpg"
    ["avatarfull"]=>
    string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/44/44780a8cb827a212dadbd0981c8834a2e1b4b7c7_full.jpg"
    ["personastate"]=>
    int(0)
    ["primaryclanid"]=>
    string(18) "103582791429521408"
    ["timecreated"]=>
    int(1411813740)
    ["personastateflags"]=>
    int(0)
    ["SteamId"]=>
    string(17) "76561198156224606"
    ["CommunityBanned"]=>
    bool(false)
    ["VACBanned"]=>
    bool(true)
    ["NumberOfVACBans"]=>
    int(1)
    ["DaysSinceLastBan"]=>
    int(1)
    ["EconomyBan"]=>
    string(4) "none"
  }
  [1]=>
  object(stdClass)#6 (11) {
    ["steamid"]=>
    string(17) "76561197961360219"
    ["communityvisibilitystate"]=>
    int(1)
    ["profilestate"]=>
    int(1)
    ["personaname"]=>
    string(9) "SVARTBERG"
    ["lastlogoff"]=>
    int(1415744339)
    ["commentpermission"]=>
    int(2)
    ["profileurl"]=>
    string(53) "http://steamcommunity.com/profiles/76561197961360219/"
    ["avatar"]=>
    string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/3e/3e649cc102a9d866272394ba321a123cb5b46c34.jpg"
    ["avatarmedium"]=>
    string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/3e/3e649cc102a9d866272394ba321a123cb5b46c34_medium.jpg"
    ["avatarfull"]=>
    string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/3e/3e649cc102a9d866272394ba321a123cb5b46c34_full.jpg"
    ["personastate"]=>
    int(0)
    ["SteamId"]=>
    string(17) "76561197961360219"
    ["CommunityBanned"]=>
    bool(false)
    ["VACBanned"]=>
    bool(false)
    ["NumberOfVACBans"]=>
    int(0)
    ["DaysSinceLastBan"]=>
    int(0)
    ["EconomyBan"]=>
    string(4) "none"
  }
}

我已经想到了另一个foreach中的一个foreach,但这对我来说似乎很奇怪而且不优雅。你有什么主意吗?我已经尝试过类似的东西:

$playerBans = getPlayerBans(array('76561198156224606', '76561197961360219'));
$playerSums = getPlayerSummaries(array('76561198156224606', '76561197961360219'));

//var_dump($playerBans, $playerSums);

$playerBans_inverted = array();

foreach ($playerBans as $index => &$subarray) {
    $playerBans_inverted[$subarray->SteamId] = $index;
}

var_dump($playerBans_inverted);

foreach ($playerSums as &$element) {
    $playerBans_element = $playerBans_inverted[$element->steamid];
    //var_dump($playerBans_element);
    $element->steamid = $playerBans_element['SteamId'];
    $element->VACBanned = $playerBans_element['VACBanned'];
}

//var_dump($playerSums);

但是这没用。此外,此代码现在还不完整,因为我无法使其正常工作。

0 个答案:

没有答案