回答編集履歴
1
add
test
CHANGED
@@ -3,3 +3,87 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
そもそもで「そういう配列」を作ればループ内でごちゃごちゃ分岐書かなくて済みます。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
雑な例
|
10
|
+
|
11
|
+
```php
|
12
|
+
|
13
|
+
<?php
|
14
|
+
|
15
|
+
$hoge = array("one", "two", "three", "two", "two");
|
16
|
+
|
17
|
+
$num = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
$ar = [];
|
22
|
+
|
23
|
+
$index = 0;
|
24
|
+
|
25
|
+
foreach($hoge as $a){
|
26
|
+
|
27
|
+
$row = [];
|
28
|
+
|
29
|
+
$row["hoge"] = $a;
|
30
|
+
|
31
|
+
$slice = [];
|
32
|
+
|
33
|
+
$slimit= 0;
|
34
|
+
|
35
|
+
switch($a){
|
36
|
+
|
37
|
+
case "one":
|
38
|
+
|
39
|
+
$slimit=1;
|
40
|
+
|
41
|
+
break;
|
42
|
+
|
43
|
+
case "two":
|
44
|
+
|
45
|
+
$slimit=2;
|
46
|
+
|
47
|
+
break;
|
48
|
+
|
49
|
+
case "three":
|
50
|
+
|
51
|
+
$slimit=3;
|
52
|
+
|
53
|
+
break;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
$slice = array_slice($num,$index,$slimit);
|
58
|
+
|
59
|
+
$index += $slimit;
|
60
|
+
|
61
|
+
$numi=1;
|
62
|
+
|
63
|
+
foreach($slice as $numi=>$n){
|
64
|
+
|
65
|
+
$row["num".$numi] = $n;
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
$ar[] = $row;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
var_dump($ar);
|
76
|
+
|
77
|
+
//あとはがんばって
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
oneとか文字になってるから分岐が増える。
|
88
|
+
|
89
|
+
構成見直したほうが良いと思う。
|