0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PHP】高等技術なコントロールブレイク

Posted at
<?php

$res = [
    ['name' => '田中',  '国語の点数' => 777],
    ['name' => '渡部',  '国語の点数' => 62],
    ['name' => '品川',  '国語の点数' => 23],
    ['name' => '品川',  '国語の点数' => 4343],
    ['name' => '滝谷',  '国語の点数' => 93],
    ['name' => '川原田','国語の点数' => 55],
    ['name' => '一色',  '国語の点数' => 27],
    ['name' => '一色',  '国語の点数' => 89],
    ['name' => '高橋',  '国語の点数' => 555],
    ['name' => '高橋',  '国語の点数' => 9999],
];


foreach ($res as $val) {
    $name = $val['name'];
    $abc = (int)$val['国語の点数'];
    if(empty($data[$name]['国語の点数'][$abc])){
        $data[$name]['国語の点数'][$abc] = $abc;
    }
}

var_dump($data);

結果

array(7) {
  ["田中"]=>
  array(1) {
    ["国語の点数"]=>
    array(1) {
      [777]=>
      int(777)
    }
  }
  ["渡部"]=>
  array(1) {
    ["国語の点数"]=>
    array(1) {
      [62]=>
      int(62)
    }
  }
  ["品川"]=>
  array(1) {
    ["国語の点数"]=>
    array(2) {
      [23]=>
      int(23)
      [4343]=>
      int(4343)
    }
  }
  ["滝谷"]=>
  array(1) {
    ["国語の点数"]=>
    array(1) {
      [93]=>
      int(93)
    }
  }
  ["川原田"]=>
  array(1) {
    ["国語の点数"]=>
    array(1) {
      [55]=>
      int(55)
    }
  }
  ["一色"]=>
  array(1) {
    ["国語の点数"]=>
    array(2) {
      [27]=>
      int(27)
      [89]=>
      int(89)
    }
  }
  ["高橋"]=>
  array(1) {
    ["国語の点数"]=>
    array(2) {
      [555]=>
      int(555)
      [9999]=>
      int(9999)
    }
  }
}

0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?