回答編集履歴

2

調整

2024/12/24 02:33

投稿

yambejp
yambejp

スコア116921

test CHANGED
@@ -30,17 +30,18 @@
30
30
  ```
31
31
  # 参考
32
32
  DOMで処理するならこんな感じ
33
+ ※一部調整
33
34
  ```PHP
34
35
  <?PHP
35
36
  function pulldown() {
36
37
  $languages = ["javascript", "PHP", "Java", "Python", "C#", "Golang", "Erlang", "Ruby"];
37
- echo implode("\n",array_map(function($value){
38
- $dom = new DOMDocument();
38
+ $dom = new DOMDocument();
39
- $x=$dom->createElement('option');
39
+ $x=$dom->createElement('option');
40
+ array_walk($languages,function($value) use($dom,$x){
40
41
  $x->textContent=$value;
41
42
  $x->setAttribute('value',$value);
42
- return $dom->saveXML($x);
43
+ print $dom->saveXML($x)."\n";
43
- },$languages));
44
+ });
44
45
  }
45
46
  ?>
46
47
 

1

調整

2024/12/24 01:24

投稿

yambejp
yambejp

スコア116921

test CHANGED
@@ -28,3 +28,38 @@
28
28
  </form>
29
29
  </html>
30
30
  ```
31
+ # 参考
32
+ DOMで処理するならこんな感じ
33
+ ```PHP
34
+ <?PHP
35
+ function pulldown() {
36
+ $languages = ["javascript", "PHP", "Java", "Python", "C#", "Golang", "Erlang", "Ruby"];
37
+ echo implode("\n",array_map(function($value){
38
+ $dom = new DOMDocument();
39
+ $x=$dom->createElement('option');
40
+ $x->textContent=$value;
41
+ $x->setAttribute('value',$value);
42
+ return $dom->saveXML($x);
43
+ },$languages));
44
+ }
45
+ ?>
46
+
47
+ <!DOCTYPE html>
48
+ <html lang='ja'>
49
+ <head>
50
+ <meta charset='utf-8'>
51
+ <title>PHP loop</title>
52
+ </head>
53
+ <body>
54
+ <form action="#">
55
+ <label for="lang">Language</label>
56
+ <select name="languages" id="lang">
57
+ <!-- 関数を出力 -->
58
+ <?php
59
+ pulldown();
60
+ ?>
61
+ </select>
62
+ <input type="submit" value="Submit" />
63
+ </form>
64
+ </html>
65
+ ```