0
0

More than 1 year has passed since last update.

【PHP】多次元配列 カラム同士を計算

Last updated at Posted at 2021-10-01
<?php
$results = [
    ['name' => '田中',  'japanese' => 83, 'math' => 57, 'science' => 43,  'social' => 72, 'english' => 78],
    ['name' => '渡部',  'japanese' => 62, 'math' => 88, 'science' => 70,  'social' => 66, 'english' => 38],
    ['name' => '品川',  'japanese' => 23, 'math' => 33, 'science' => 53,  'social' => 17, 'english' => 07],
    ['name' => '滝谷',  'japanese' => 93, 'math' => 89, 'science' => 79,  'social' => 96, 'english' => 91],
    ['name' => '川原田','japanese' => 55, 'math' => 56, 'science' => 60,  'social' => 54, 'english' => 66],
    ['name' => '一色',  'japanese' => 27, 'math' => 47, 'science' => 100, 'social' => 42, 'english' => 38],
];
$value = array_map(function($num){
    return [$num['name'] =>  array_sum($num)];
}, $results);

var_dump($value);

#結果

array(6) {
  [0]=>
  array(1) {
    ["田中"]=>
    int(333)
  }
  [1]=>
  array(1) {
    ["渡部"]=>
    int(324)
  }
  [2]=>
  array(1) {
    ["品川"]=>
    int(133)
  }
  [3]=>
  array(1) {
    ["滝谷"]=>
    int(448)
  }
  [4]=>
  array(1) {
    ["川原田"]=>
    int(291)
  }
  [5]=>
  array(1) {
    ["一色"]=>
    int(254)
  }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0