2016-10-03 132 views
-1
$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", 
     ), 
    ); 

預期輸出:PHP組多維數組?

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

我混淆有關如何使用多維到組陣列按預期輸出。我的高級建議我使用多維數組,因爲我嘗試了很多次,但我不知道如何分組數組,請問有人能幫助我!

回答

1
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); 
+0

@nice ...但是這會給high_score爲10000 ID 2是14000,但整體的短期和快速:) +1簡短的回答 – user1234

+0

高分也可以用一個條件每次迭代 – Shahrukh

+0

感謝檢查求助! –

0

嗨試試這個。

$output = array(); 
foreach($arr as $array){  
    if(array_key_exists($array['id'],$output)){ 
     $output[$array['id']][0]['type'] = $output[$array['id']][0]['type'].','.$array['type']; 
     $output[$array['id']][0]['high_score'] = $output[$array['id']][0]['high_score'] > $array['high_score'] ? $output[$array['id']][0]['high_score'] : $array['high_score']; 
     $output[$array['id']][0]['score'] = $output[$array['id']][0]['score'] + $array['score']; 

    }else { 
     $output[$array['id']][] = $array; 
     } 

} 

echo "<pre>";print_r($output); 

輸出

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 
       ) 

     ) 

) 
0

這裏是您的解決方案。

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

但是,如果我添加另一個數組喜歡: - 數組(「id」=>「1」,「player_name」=>「標記」,「類型」=>「賽車」,「high_score」=>「12000」 「=>」500「,),第一個記錄名稱變成標記,任何想法將第1組數據分成2個記錄,即第1組有2個記錄,例如千斤頂和標記? –

+0

感謝您的幫助! –