PHP组多维数组?
预期产出:PHP组多维数组?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,预期产出: $arr = Array ( Array( "id" => "1", "player_name" => "Jack", "type" => "Arcades", "high_score"=> "10000", "score"=> "500", ), Array( "id"
$arr = Array
(
Array(
"id" => "1",
"player_name" => "Jack",
"type" => "Arcades",
"high_score"=> "10000",
"score"=> "500",
),
Array(
"id" => "1",
"player_name" => "Jack",
"type" => "Racing",
"high_score"=> "12000",
"score"=> "700",
),
Array(
"id" => "2",
"player_name" => "Steve",
"type" => "Racing",
"high_score"=> "10000",
"score"=> "400",
),
Array(
"id" => "2",
"player_name" => "Steve",
"type" => "Arcades",
"high_score"=> "12000",
"score"=> "600",
),
Array(
"id" => "2",
"player_name" => "Steve",
"type" => "Casual",
"high_score"=> "14000",
"score"=> "650",
),
Array(
"id" => "2",
"player_name" => "Steve",
"type" => "Strategy",
"high_score"=> "10000",
"score"=> "500",
),
);
我对如何使用多维数组将数组按预期输出分组感到困惑。我的前辈建议我使用多维数组,因为我已经尝试过很多次了,但我不知道如何对数组进行分组,有人能帮我吗 试试这个:
$arr = Array (
'1'=>
Array(
"id" => "1",
"player_name" => "Jack",
"type" => "Arcades,Racing",
"high_score"=> "12000",
"score"=> "1200",
),
'2'=>
Array(
"id" => "2",
"player_name" => "Steve",
"type" => "Racing,Arcades,Casual,Strategy",
"high_score"=> "14000",
"score"=> "2150",
),
);
foreach($arr as $val)
{
$newarray[$val['id']]['id']=$val['id'];
$newarray[$val['id']]['player_name']=$val['player_name'];
$newarray[$val['id']]['type']=isset($newarray[$val['id']]['type'])?$newarray[$val['id']]['type'].','.$val['type']:$val['type'];
$newarray[$val['id']]['high_score']=$val['high_score'];
$newarray[$val['id']]['score']=isset($newarray[$val['id']]['score'])?$val['score']+$newarray[$val['id']]['score']:$val['score'];
}
print_r($newarray);
这是你的解决方案
$res = [];
$highScore = [];
$type = [];
$score= [];
foreach($arr as $val){
$highScore[$val['player_name']][] = $val['high_score'];
$type[$val['player_name']][] = $val['type'];
$score[$val['player_name']][] = $val['score'];
$res[$val['id']] = $val;
$res[$val['id']]['high_score'] = !empty($highScore[$val['player_name']])? max($highScore[$val['player_name']]) : '';
$res[$val['id']]['type'] = !empty($type[$val['player_name']]) ? implode(',',$type[$val['player_name']]) : '';
$res[$val['id']]['score'] = !empty($score[$val['player_name']]) ? array_sum($score[$val['player_name']]) : 0;
}
echo '<pre>'; print_r($res);
$res=[];
$highScore=[];
$type=[];
$score=[];
外汇($arr作为$val){
$highScore[$val['player_name']][=$val['high_score'];
$type[$val['player_name'][]=$val['type'];
$score[$val['player_name'][]=$val['score'];
$res[$val['id']]=$val;
$res[$val['id']['high_score']=!empty($highScore[$val['player_name']])?max($highScore[$val['player_name']]):“”;
$res[$val['id']['type']=!empty($type[$val['player\u name']])?内爆(“,”,$type[$val['player\u name']]):”;
$res[$val['id']['score']=!空($score[$val['player\u name']])?数组求和($score[$val['player\u name']]):0;
}
回声';印刷品(港币);;
@nice。。。但是这会给ID2带来10000分的高分,这是14000分,但总体上是简短而快速的:)+1分的简短回答也可以在每次迭代中用一个条件检查高分,但是如果我添加另一个数组,比如:-数组(“id”=>“1”,“玩家名”=>“Mark”,“键入”=>“Racing”,“高分”=>“12000”,“分数”=>“500”),第一个记录名成为Mark,有没有想法将组1数组分隔为2个记录,即组1有2个记录,例如jack和Mark?
Array
(
[1] => Array
(
[0] => Array
(
[id] => 1
[player_name] => Jack
[type] => Arcades,Racing
[high_score] => 12000
[score] => 1200
)
)
[2] => Array
(
[0] => Array
(
[id] => 2
[player_name] => Steve
[type] => Racing,Arcades,Casual,Strategy
[high_score] => 14000
[score] => 2150
)
)
)
$res = [];
$highScore = [];
$type = [];
$score= [];
foreach($arr as $val){
$highScore[$val['player_name']][] = $val['high_score'];
$type[$val['player_name']][] = $val['type'];
$score[$val['player_name']][] = $val['score'];
$res[$val['id']] = $val;
$res[$val['id']]['high_score'] = !empty($highScore[$val['player_name']])? max($highScore[$val['player_name']]) : '';
$res[$val['id']]['type'] = !empty($type[$val['player_name']]) ? implode(',',$type[$val['player_name']]) : '';
$res[$val['id']]['score'] = !empty($score[$val['player_name']]) ? array_sum($score[$val['player_name']]) : 0;
}
echo '<pre>'; print_r($res);