PHP: Merge two Arrays of objects by same value

350 Views Asked by At

I got these two things:

Array1

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)
  }
}

As you can see, both arrays hold objects which hold one same value (SteamId / steamid). No I want to merge these arrays of objects so that I got something like this:

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"
  }
}

I already thought of one foreach in another foreach but that seems weird and not elegant to me. Do you have any idea? I already tried something like that:

$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);

But this didn't work. Plus this code is incomplete now because I could't make it work correctly.

0

There are 0 best solutions below