質問編集履歴

1

ご回答いただいた内容において、配列にURLを追加しました。

2022/06/08 11:56

投稿

kakashi55
kakashi55

スコア25

test CHANGED
File without changes
test CHANGED
@@ -46,3 +46,45 @@
46
46
  <?php endforeach; ?>
47
47
  </table>
48
48
  ```
49
+
50
+ ```URLリンクを配列に追加
51
+ $data = [
52
+ ["color" => "red", "things" => "りんご", "place" => "青森", "url" => "https://www.yahoo.co.jp/"],
53
+ ["color" => "red", "things" => "信号", "place" => "東京", "url" => "https://www.youtube.com/"],
54
+ ["color" => "red", "things" => "太陽", "place" => "テキサス", "url" => "https://ja.wikipedia.org/wiki/"],
55
+ ["color" => "green", "things" => "山", "place" => "山形", "url" => "https://www.amazon.co.jp/"],
56
+ ["color" => "green", "things" => "森", "place" => "静岡", "url" => "https://www.dazn.com/"],
57
+ ];
58
+
59
+ $result = array_reduce(array_unique(array_map(function ($x) {
60
+ return $x["color"];
61
+ }, $data)), function ($x, $y) use ($data) {
62
+ $x[$y] = array_map(function ($x) {
63
+ unset($x["color"]);
64
+ return $x;
65
+ }, array_values(array_filter($data, function ($x) use ($y) {
66
+ return $x["color"] == $y;
67
+ })));
68
+ return $x;
69
+ }, []);
70
+
71
+
72
+ print_r($result);
73
+ print "<table border>\n";
74
+ print "<thead>\n<tr><th>color</th><th>things</th><th>place</th></tr>\n</thead>\n";
75
+ print "<tbody>\n";
76
+ foreach ($result as $color => $vals) {
77
+ print "<tr>";
78
+ print "<td rowspan=\"" . count($vals) . "\">$color</td>";
79
+ foreach ($vals as $key => $val) {
80
+ if ($key > 0) {
81
+ print "</tr>\n<tr>";
82
+ }
83
+ print "<td>${val["things"]}</td>";
84
+ print "<td>${val["place"]}</td>";
85
+ }
86
+ print "</tr>\n";
87
+ }
88
+ print "</tbody>\n";
89
+ print "</table>\n";
90
+ ```