データ架型:
{"game_id":"111","type":2,"num":504},{"game_id":"111","type":6,"num":8154}, {"game_id":"111","type":41,"num":3426},{"game_id":"111","type":42,"num":45},{"game_id":" 111","type":43,"num":1873}
要件:
ハンドルtype=41 42 42的num相加 type 1 10相加
結果:
{"game_id":"111 ","type":2,"num":504},{"game_id":"111","type":6,"num":8154},{"game_id":"111","type": 41,"num":5344}
コード:
$str = '{"retCode":"0","retMsg":"成功","data":[{"game_id":"111" ,"タイプ":2,"番号":504},{"ゲームID":"111","タイプ":6,"番号":8154},{"ゲームID":"111","タイプ":41 ,"番号":3426},{"ゲームID":"111","タイプ":42,"番号":45},{"ゲームID":"111","タイプ":43,"番号":1873 },{"ゲームID":"119","タイプ":1,"番号":38},{"ゲームID":"119","タイプ":2,"番号":381},{"ゲームID" :"119","タイプ":6,"数値":189},{"ゲームID":"119","タイプ":10,"数値":21},{"ゲームID":"120"," type":2,"num":31},{"game_id":"120","type":6,"num":291},{"game_id":"120","type":41," num":90},{"game_id":"120","type":43,"num":49},{"game_id":"127","type":1,"num":9}, {"game_id":"127","type":2,"num":111},{"game_id":"127","type":6,"num":60},{"game_id":" 129","タイプ":1,"番号":11},{"ゲームID":"129","タイプ":2,"番号":11},{"ゲームID":"129","タイプ" :6,"数値":48},{"ゲームID":"129","タイプ":10,"数値":2},{"ゲームID":"130","タイプ":6,"数値" :1},{"ゲーム ID":"137","タイプ":2,"数値":1},{"ゲーム ID":"139","タイプ":2,"数値":1387},{"ゲーム ID":"139","タイプ":6,"数値":3858},{"ゲーム ID":"139","タイプ":10,"数値":2358},{"ゲーム ID":"142" ,"タイプ":2,"番号":32},{"ゲームID":"142","タイプ":6,"番号":948},{"ゲームID":"142","タイプ":41 ,"番号":330},{"ゲームID":"142","タイプ":42,"番号":3},{"ゲームID":"142","タイプ":43,"番号":47 },{"ゲームID":"156","タイプ":2,"数値":11436},{"ゲームID":"156","タイプ":6,"数値":38135},{"ゲームID" :"156","タイプ":10,"数値":22170},{"ゲームID":"165","タイプ":6,"数値":6},{"ゲームID":"165"," type":41,"num":3},{"game_id":"165","type":43,"num":1},{"game_id":"50","type":2," num":3},{"game_id":"8f093ab4ad19a509208a8104605c6e42","type":2,"num":281},{"game_id":"92","type":1,"num":26}, {"game_id":"92","type":2,"num":186},{"game_id":"92","type":6,"num":153},{"game_id":" 94","タイプ":1,"番号":538},{"ゲームID":"94","タイプ":2,"番号":3357},{"ゲームID":"94","タイプ" :6,"num":2847},{"game_id":"94","type":10,"num":74}]}';
$aaa = json_decode($str, true); $data = $aaa['data']; $new_array = array(); $array1 = array(41,42,43); $array2 = array(1,10); foreach($data as $v){ if(in_array($v['type'], $array2)){ if(isset($new_array[$v['game_id']][$array2[0]]['num'])){ $new_array[$v['game_id']][$array2[0]]['num'] += $v['num']; } else { $new_array[$v['game_id']][$array2[0]]['num'] = $v['num']; } }elseif (in_array($v['type'], $array1)) { if(isset($new_array[$v['game_id']][$array1[0]]['num'])){ $new_array[$v['game_id']][$array1[0]]['num'] += $v['num']; } else { $new_array[$v['game_id']][$array1[0]]['num'] = $v['num']; } }else{ if(isset($new_array[$v['game_id']][$v['type']]['num'])){ $new_array[$v['game_id']][$v['type']]['num'] += $v['num']; } else { $new_array[$v['game_id']][$v['type']]['num'] = $v['num']; } } } foreach($new_array as $k1 => $v1) { foreach($v1 as $k2 => $v2) { $new[$k1][] = array( 'game_id' => $k1, 'type' => $k2, 'num' => $v2['num'] ); } }
以上は、特定のキーグループに基づいて php を計算し、側面の内容を含めたもので、PHP 教則に関心のある友人の助けになることを望みます。