回答編集履歴

1

追記

2019/09/08 07:34

投稿

退会済みユーザー
test CHANGED
@@ -7,3 +7,81 @@
7
7
 
8
8
 
9
9
  なんかリファクタリングしたぃ~って感じなら、テストいっぱい書いて、検証しながら配列を比較する関数と置き換えてみると良いです。
10
+
11
+
12
+
13
+ **sample**
14
+
15
+ ```php
16
+
17
+ <?php
18
+
19
+ $test_arrays = [
20
+
21
+ ['apple', 'xxx'],
22
+
23
+ ['apple', 'melon'],
24
+
25
+ ['melon', 'apple'],
26
+
27
+ ['apple', ''],
28
+
29
+ ['apple', NULL],
30
+
31
+ ['apple', 0],
32
+
33
+ ];
34
+
35
+
36
+
37
+ function test($test_array){
38
+
39
+ $ok_arr = [
40
+
41
+ 'apple' => true,
42
+
43
+ 'melon' => true,
44
+
45
+ ];
46
+
47
+ foreach($test_array as $str){
48
+
49
+ if(!isset($ok_arr[$str])){
50
+
51
+ return false;
52
+
53
+ }
54
+
55
+ }
56
+
57
+ return true;
58
+
59
+ }
60
+
61
+ foreach($test_arrays as $test_array){
62
+
63
+ echo (join(",", $test_array)) . ':';
64
+
65
+ var_export(test($test_array));
66
+
67
+ echo PHP_EOL;
68
+
69
+ }
70
+
71
+ ```
72
+
73
+ ```
74
+
75
+ apple,xxx:false
76
+
77
+ apple,melon:true
78
+
79
+ melon,apple:true
80
+
81
+ apple,:false
82
+
83
+ apple,:false
84
+
85
+ apple,0:false
86
+
87
+ ```