如何在PHP中组合具有相同关联键值的数组?
我试图通过wordpress中的帖子循环创建一个数组,并构建一个由人、他们玩过的游戏以及他们每场游戏发布的分数组成的数组 我当前的代码:如何在PHP中组合具有相同关联键值的数组?,php,arrays,wordpress,associative-array,Php,Arrays,Wordpress,Associative Array,我试图通过wordpress中的帖子循环创建一个数组,并构建一个由人、他们玩过的游戏以及他们每场游戏发布的分数组成的数组 我当前的代码: $world_rank = []; while ( $the_query->have_posts() ) : $the_query->the_post(); $category = get_the_category(); $cat_name = $category[0]->cat_nam
$world_rank = [];
while ( $the_query->have_posts() ) : $the_query->the_post();
$category = get_the_category();
$cat_name = $category[0]->cat_name;
$score = get_field('score');
$author = get_the_author();
$array_size = sizeof($world_rank[$author]['games']);
if( $array_size < 1 ){
$world_rank[$author]['games'][] = ['name' => addslashes($cat_name), 'scores' => [$score] ];
}else{
// loop through the array and see if they have been similar game names if so, add them to push array
$i = 0;
foreach($world_rank[$author]['games'] as $game){
if($game['name'] == $cat_name){
echo 'Same name ' .$game['scores']. ' score is '.$score.'</br>';
array_push($game['scores'], $score);
// this doesn't seem to work
// break;
}
else if($game['name'] !== $cat_name && $i == $array_size - 1 ){ //
// if the names are not the same create a new array for the other game
$world_rank[$author]['games'][] = ['name' => addslashes($cat_name), 'scores' => [$score] ];
}
$i++;
}
}
endwhile;
如何使world_rank数组中的“Candy”值为1数组,因为“Candy”值在数组中出现两次,并将“Candy”键的分数合并。所以现在让它看起来像:
$world_rank = array (
'frantheman' =>
array (
'games' =>
array (
0 =>
array (
'name' => 'Candy',
'scores' =>
array (
0 => '23',
1 => '25'
),
),
1 =>
array (
'name' => 'PopCorn',
'scores' =>
array (
0 => '25',
),
),
2 =>
array (
'name' => 'Chocolate',
'scores' =>
array (
0 => '5',
),
)
);
如您所见,在新的数组中,“Candy”有两个用于分数的数组。在此方面的任何帮助都将不胜感激。谢谢。您的代码中有一个小错误。如果你想在foreach中修改$game,你需要通过引用遍历它。你只需要做一件事:
foreach($world_rank[$author]['games'] as &$game){
然后实际上修改实际分数数组,而不创建本地副本。(如果您想了解更多信息,请使用Google PHP copy on write)
您还需要break语句,否则将在循环结束条件中创建另一个元素。但你已经把它放在了正确的地方;联合国对此发表评论
备选方案
我在下面添加的选项是错误的删除。编辑在与OP反复讨论之后,我重写了我的答案和OP的原始脚本 这是可测试脚本:() 输出:
array (
'frantheman' =>
array (
'Candy Crush' =>
array (
'scores' =>
array (
0 => 11,
1 => 5,
2 => 4,
3 => 23,
4 => 25,
5 => 0,
),
'high_score' => 25,
'rank' => 2,
'efficiency' => 10.0,
),
'Blackjack' =>
array (
'scores' =>
array (
0 => 68,
1 => 0,
2 => 90,
),
'high_score' => 90,
'rank' => 1,
'efficiency' => 5.0,
),
'Killer Instinct' =>
array (
'scores' =>
array (
0 => 25,
),
'high_score' => 25,
'rank' => 1,
'efficiency' => 10.0,
),
'Grand Theft Auto V' =>
array (
'scores' =>
array (
0 => 5,
),
'high_score' => 5,
'rank' => 2,
'efficiency' => 10.0,
),
'Counter-Strike: Global Offensive' =>
array (
'scores' =>
array (
0 => 67,
),
'high_score' => 67,
'rank' => 2,
'efficiency' => 10.0,
),
'Lawbreakers' =>
array (
'scores' =>
array (
0 => 456,
),
'high_score' => 456,
'rank' => 2,
'efficiency' => 10.0,
),
),
'Braconda' =>
array (
'Overwatch' =>
array (
'scores' =>
array (
0 => 24459,
1 => 70,
),
'high_score' => 24459,
'rank' => 1,
'efficiency' => 5.0,
),
),
'ScotGamer1' =>
array (
'Overwatch' =>
array (
'scores' =>
array (
0 => 24359,
),
'high_score' => 24359,
'rank' => 2,
'efficiency' => 10.0,
),
'Blackjack' =>
array (
'scores' =>
array (
0 => 21,
),
'high_score' => 21,
'rank' => 2,
'efficiency' => 10.0,
),
'Grand Theft Auto V' =>
array (
'scores' =>
array (
0 => 23386,
),
'high_score' => 23386,
'rank' => 1,
'efficiency' => 5.0,
),
'Dota 2' =>
array (
'scores' =>
array (
0 => 2560,
),
'high_score' => 2560,
'rank' => 2,
'efficiency' => 10.0,
),
),
'John' =>
array (
'Counter-Strike: Global Offensive' =>
array (
'scores' =>
array (
0 => 75656,
1 => 19000,
),
'high_score' => 75656,
'rank' => 1,
'efficiency' => 5.0,
),
'Team Fortress 2' =>
array (
'scores' =>
array (
0 => 8686,
1 => 7755,
),
'high_score' => 8686,
'rank' => 1,
'efficiency' => 10.0,
),
'Dota 2' =>
array (
'scores' =>
array (
0 => 86766,
1 => 12345,
),
'high_score' => 86766,
'rank' => 1,
'efficiency' => 5.0,
),
'Candy Crush' =>
array (
'scores' =>
array (
0 => 9999999999999999,
1 => 66,
),
'high_score' => 9999999999999999,
'rank' => 1,
'efficiency' => 5.0,
),
'PLAYERUNKNOWN’S BATTLEGROUNDS' =>
array (
'scores' =>
array (
0 => 42,
),
'high_score' => 42,
'rank' => 1,
'efficiency' => 10.0,
),
'Lawbreakers' =>
array (
'scores' =>
array (
0 => 5345345,
1 => 54321,
),
'high_score' => 5345345,
'rank' => 1,
'efficiency' => 5.0,
),
),
)
这是我的代码片段的WP实现:
$user_game_stats=[]; // this will be the final output array containing all statistics
while($the_query->have_posts()){
$the_query->the_post();
$game = addslashes(get_the_category()[0]->cat_name);
$user = get_the_author();
$score=intval(get_field('score'));
$user_game_stats[$user][$game]['scores'][]=$score;
if(!isset($user_game_stats[$user][$game]['high_score']) || $score>$user_game_stats[$user][$game]['high_score']){
$user_game_stats[$user][$game]['high_score']=$score;
$user_high_scores_by_game[$game][$user]=$score; // this is a temporary array for calculating rank
}
}
// a second loop is essential because the rank calculations involve the users' high scores and player count per game
foreach($user_high_scores_by_game as $game=>$user_scores){
$total_game_users=count($user_scores);
arsort($user_scores); // sort the users by their high score in DESC order and preserve the keys (usernames)
$ordered_users=array_keys($user_scores); // high scores are no longer required, we just want to determine rank
foreach($ordered_users as $number=>$user){
$user_game_stats[$user][$game]['rank']=++$number; // starts from zero so $number is modified/increased by one for human-friendly rank
$user_game_stats[$user][$game]['efficiency']=$number / ($total_game_users * .1); // I don't like "efficiency" but can't offer a better term
// you may want to introduce rounding on the efficiency calculation
}
}
var_export($user_game_stats);
请提供代码,您如何创建第一个array@Tarasovych它已经更新。谢谢你的评论让我把它放进去,不像其他人那样投反对票。你必须用
array_push()
,使用foreach()
把所有游戏的分数放进exist数组,谢谢,但我就是这么做的。在这里:f($game['name]=$cat_name){echo'同名。$game['scores]“'score is”“$score”“”;array_push($game['scores',$score);//这似乎不起作用//break;}如果我在该条件下执行回显,我会看到它在“Candy”name键处添加到数组中。然而,当我打印数组的最终结果时,我得到了问题中显示的第一个输出。
array (
'frantheman' =>
array (
'Candy Crush' =>
array (
'scores' =>
array (
0 => 11,
1 => 5,
2 => 4,
3 => 23,
4 => 25,
5 => 0,
),
'high_score' => 25,
'rank' => 2,
'efficiency' => 10.0,
),
'Blackjack' =>
array (
'scores' =>
array (
0 => 68,
1 => 0,
2 => 90,
),
'high_score' => 90,
'rank' => 1,
'efficiency' => 5.0,
),
'Killer Instinct' =>
array (
'scores' =>
array (
0 => 25,
),
'high_score' => 25,
'rank' => 1,
'efficiency' => 10.0,
),
'Grand Theft Auto V' =>
array (
'scores' =>
array (
0 => 5,
),
'high_score' => 5,
'rank' => 2,
'efficiency' => 10.0,
),
'Counter-Strike: Global Offensive' =>
array (
'scores' =>
array (
0 => 67,
),
'high_score' => 67,
'rank' => 2,
'efficiency' => 10.0,
),
'Lawbreakers' =>
array (
'scores' =>
array (
0 => 456,
),
'high_score' => 456,
'rank' => 2,
'efficiency' => 10.0,
),
),
'Braconda' =>
array (
'Overwatch' =>
array (
'scores' =>
array (
0 => 24459,
1 => 70,
),
'high_score' => 24459,
'rank' => 1,
'efficiency' => 5.0,
),
),
'ScotGamer1' =>
array (
'Overwatch' =>
array (
'scores' =>
array (
0 => 24359,
),
'high_score' => 24359,
'rank' => 2,
'efficiency' => 10.0,
),
'Blackjack' =>
array (
'scores' =>
array (
0 => 21,
),
'high_score' => 21,
'rank' => 2,
'efficiency' => 10.0,
),
'Grand Theft Auto V' =>
array (
'scores' =>
array (
0 => 23386,
),
'high_score' => 23386,
'rank' => 1,
'efficiency' => 5.0,
),
'Dota 2' =>
array (
'scores' =>
array (
0 => 2560,
),
'high_score' => 2560,
'rank' => 2,
'efficiency' => 10.0,
),
),
'John' =>
array (
'Counter-Strike: Global Offensive' =>
array (
'scores' =>
array (
0 => 75656,
1 => 19000,
),
'high_score' => 75656,
'rank' => 1,
'efficiency' => 5.0,
),
'Team Fortress 2' =>
array (
'scores' =>
array (
0 => 8686,
1 => 7755,
),
'high_score' => 8686,
'rank' => 1,
'efficiency' => 10.0,
),
'Dota 2' =>
array (
'scores' =>
array (
0 => 86766,
1 => 12345,
),
'high_score' => 86766,
'rank' => 1,
'efficiency' => 5.0,
),
'Candy Crush' =>
array (
'scores' =>
array (
0 => 9999999999999999,
1 => 66,
),
'high_score' => 9999999999999999,
'rank' => 1,
'efficiency' => 5.0,
),
'PLAYERUNKNOWN’S BATTLEGROUNDS' =>
array (
'scores' =>
array (
0 => 42,
),
'high_score' => 42,
'rank' => 1,
'efficiency' => 10.0,
),
'Lawbreakers' =>
array (
'scores' =>
array (
0 => 5345345,
1 => 54321,
),
'high_score' => 5345345,
'rank' => 1,
'efficiency' => 5.0,
),
),
)
$user_game_stats=[]; // this will be the final output array containing all statistics
while($the_query->have_posts()){
$the_query->the_post();
$game = addslashes(get_the_category()[0]->cat_name);
$user = get_the_author();
$score=intval(get_field('score'));
$user_game_stats[$user][$game]['scores'][]=$score;
if(!isset($user_game_stats[$user][$game]['high_score']) || $score>$user_game_stats[$user][$game]['high_score']){
$user_game_stats[$user][$game]['high_score']=$score;
$user_high_scores_by_game[$game][$user]=$score; // this is a temporary array for calculating rank
}
}
// a second loop is essential because the rank calculations involve the users' high scores and player count per game
foreach($user_high_scores_by_game as $game=>$user_scores){
$total_game_users=count($user_scores);
arsort($user_scores); // sort the users by their high score in DESC order and preserve the keys (usernames)
$ordered_users=array_keys($user_scores); // high scores are no longer required, we just want to determine rank
foreach($ordered_users as $number=>$user){
$user_game_stats[$user][$game]['rank']=++$number; // starts from zero so $number is modified/increased by one for human-friendly rank
$user_game_stats[$user][$game]['efficiency']=$number / ($total_game_users * .1); // I don't like "efficiency" but can't offer a better term
// you may want to introduce rounding on the efficiency calculation
}
}
var_export($user_game_stats);