Stack Overflow на русском Asked by spl33t on February 15, 2021
Есть два масcива которые нужно объединить по одинаковому значению ключа, [SteamID64] в первой таблице и [steamid] во второй таблице.
В первом массиве [SteamID64] может быть одинаковый в примере я как раз описал этот случай.
Первый массив
$arr1 = [
'0' => [
"PlayerID" => "1",
"User" => "lexa",
"Time" => "300",
"SteamID64" => "76561198170801830",
"SteamID" => "STEAM_0:0:105268051"
],
'1' => [
"PlayerID" => "1",
"User" => "lexa",
"Time" => "250",
"SteamID64" => "76561198170801830",
"SteamID" => "STEAM_0:0:105268051"
],
'2'=> [
'PlayerID' => "5",
'User' => "Player",
"Time" => "400",
'SteamID64' => "76561199604582870",
'SteamI' => "STEAM_0:0:822158571"
],
'3' => [
"PlayerID" => "4",
"User" => "idfyau",
"Time" => "360",
"SteamID64" => "76561198161729178",
"SteamID" => "STEAM_0:0:100731725"
]
];
Второй массив
$arr2 = [
'76561198170801830'=> [
"steamid" => "76561198170801830",
"personaname" => "Black_Mens___",
"profileurl" => "https://steamcommunity.com/id/3523557555/",
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/64/64d96db5c4a160205149bbfab2ccdbc7c3bad688.jpg"
],
'76561198161729178'=> [
"steamid"=> "76561198161729178",
"personaname"=> "ак4ОнаиД",
"profileurl"=> "https://steamcommunity.com/id/dfhfdj/",
"avatar"=> "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/bf/bfa372987ba46f33b75ea1078e5b0a0a001cb2d8.jpg"
]
];
Желаемый результат* для игрока с [PlayerID] => 5 не было найдено пары поэтому он попадает в результат каким и был.
$data = [
'0' => [
"PlayerID" => "1",
"User" => "lexa",
"Time" => "300",
"SteamID64" => "76561198170801830",
"SteamID" => "STEAM_0:0:105268051",
"personaname" => "Black_Mens___",
"profileurl" => "https://steamcommunity.com/id/3523557555/",
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/64/64d96db5c4a160205149bbfab2ccdbc7c3bad688.jpg"
],
'1' => [
"PlayerID" => "1",
"User" => "lexa",
"Time" => "250",
"SteamID64" => "76561198170801830",
"SteamID" => "STEAM_0:0:105268051",
"personaname" => "Black_Mens___",
"profileurl" => "https://steamcommunity.com/id/3523557555/",
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/64/64d96db5c4a160205149bbfab2ccdbc7c3bad688.jpg"
],
'2'=> [
'PlayerID' => "5",
'User' => "Player",
"Time" => "400",
'SteamID64' => "76561199604582870",
'SteamI' => "STEAM_0:0:822158571"
],
'3' => [
"PlayerID" => "4",
"User" => "idfyau",
"Time" => "360",
"SteamID64" => "76561198161729178",
"SteamID" => "STEAM_0:0:100731725",
"personaname" => "ак4ОнаиД",
"profileurl" => "https://steamcommunity.com/id/dfhfdj/",
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/bf/bfa372987ba46f33b75ea1078e5b0a0a001cb2d8.jpg"
]
];
Сейчас я делаю это так:
foreach ($arr1 as $key => $row1) {
foreach ($arr2 as $key => $row2) {
if($row1['SteamID64'] == $row2['steamid'] ){
$row1['SteamID64'] = $row2['steamid'];
$row1['personaname'] = $row2['personaname'];
$row1['profileurl'] = $row2['profileurl'];
$row1['avatar'] = $row2['avatar'];
$row1['avatarmedium'] = $row2['avatarmedium'];
$row1['avatarfull'] = $row2['avatarfull'];
$data[] = $row1;
break;
}
}
}
И получаю это Для игрока с [PlayerID] => 5 не было найдено пары и его нету в результате
$data = [
'0' => [
"PlayerID" => "1",
"User" => "lexa",
"Time" => "300",
"SteamID64" => "76561198170801830",
"SteamID" => "STEAM_0:0:105268051"
"personaname" => "Black_Mens___"
"profileurl" => "https://steamcommunity.com/id/3523557555/"
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/64/64d96db5c4a160205149bbfab2ccdbc7c3bad688.jpg"
],
'1' => [
"PlayerID" => "1",
"User" => "lexa",
"Time" => "250",
"SteamID64" => "76561198170801830",
"SteamID" => "STEAM_0:0:105268051"
"personaname" => "Black_Mens___"
"profileurl" => "https://steamcommunity.com/id/3523557555/"
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/64/64d96db5c4a160205149bbfab2ccdbc7c3bad688.jpg"
],
'2' => [
"PlayerID" => "4",
"User" => "idfyau",
"Time" => "360",
"SteamID64" => "76561198161729178",
"SteamID" => "STEAM_0:0:100731725"
"personaname" => "ак4ОнаиД"
"profileurl" => "https://steamcommunity.com/id/dfhfdj/"
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/bf/bfa372987ba46f33b75ea1078e5b0a0a001cb2d8.jpg"
]
];
Проблема моего кода в том что в результат попадают только те для кого была найдена пара. Мне бы хотелось получать всех игроков у которых есть пара и у которых ее нету, как я показал в желаемом результате*.
А если попробовать вот так?
foreach ($arr1 as $key => $row1) {
$temp = $row1;
if (array_key_exists($row1['SteamID64'], $arr2)) {
$temp += $arr2[$row1['SteamID64']];
}
$data[] = $temp;
}
Correct answer by ArchDemon on February 15, 2021
`
$arr1 = [
'0' => [
"PlayerID" => "1",
"User" => "lexa",
"SteamID64" => "76561198170801830",
"SteamID" => "STEAM_0:0:105268051"
],
'1' => [
"PlayerID" => "4",
"User" => "idfyau",
"SteamID64" => "76561198161729178",
"SteamID" => "STEAM_0:0:100731725"
],
'2'=> [
'PlayerID' => "5",
'User' => "Player",
'SteamID64' => "76561199604582870",
'SteamI' => "STEAM_0:0:822158571"
]
];
$arr2 = [
'76561198170801830'=> [
"steamid" => "76561198170801830",
"personaname" => "Black_Mens___",
"profileurl" => "https://steamcommunity.com/id/3523557555/",
"avatar" => "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/64/64d96db5c4a160205149bbfab2ccdbc7c3bad688.jpg"
],
'76561198161729178'=> [
"steamid"=> "76561198161729178",
"personaname"=> "ак4ОнаиД",
"profileurl"=> "https://steamcommunity.com/id/dfhfdj/",
"avatar"=> "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/bf/bfa372987ba46f33b75ea1078e5b0a0a001cb2d8.jpg"
]
];
$arr3 = [];
foreach ($arr1 as $key1 => $row1) {
foreach($arr2 as $key2 => $row2) {
if($row1['SteamID64'] == $key2) {
$arr3[$key2] = array_merge($row1, $row2);
}
}
}
echo '<pre>';
print_r($arr3);
echo '</pre>';`
Answered by Эльдар Дадашов on February 15, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP