teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2016/08/24 23:57

投稿

ttyp03
ttyp03

スコア17002

answer CHANGED
@@ -66,5 +66,96 @@
66
66
  }
67
67
  }
68
68
  }
69
+ ```
69
70
 
71
+ ---
72
+ 追記
73
+ ```PHP
74
+ $food1 = "猫缶"; //$_POST['food1']; //1つ目のselectのoptionのvalue
75
+ $food2 = "カリカリ"; //$_POST['food2']; //2つ目のselectのoptionのvalue
76
+
77
+ // 検索結果をショップ名をキーにした連想配列に詰める
78
+ $result["shop-a"][$food1][] = array("item"=>"猫缶A","price"=>100,"url"=>"http://...");
79
+ $result["shop-a"][$food1][] = array("item"=>"猫缶A(関東限定)","price"=>100,"url"=>"http://...");
80
+ $result["shop-a"][$food2][] = array("item"=>"猫缶B","price"=>150,"url"=>"http://...");
81
+ $result["shop-b"][$food1][] = array("item"=>"猫缶A","price"=>105,"url"=>"http://...");
82
+ $result["shop-c"][$food1][] = array("item"=>"猫缶A","price"=>98,"url"=>"http://...");
83
+ $result["shop-c"][$food2][] = array("item"=>"猫缶B","price"=>110,"url"=>"http://...");
84
+
85
+ // food1とfood2の両方の結果があるショップだけを抽出する
86
+ $result2 = array_filter($result, function($r){
87
+ return count($r)>=2;
88
+ });
89
+
90
+ // 並び替えの処理は、複数商品があった場合どうするかによるので今回は省略
91
+
92
+ var_dump($result2);
93
+ ```
94
+ 結果
95
+ ```
96
+ array(2) {
97
+ ["shop-a"]=>
98
+ array(2) {
99
+ ["猫缶"]=>
100
+ array(2) {
101
+ [0]=>
102
+ array(3) {
103
+ ["item"]=>
104
+ string(7) "猫缶A"
105
+ ["price"]=>
106
+ int(100)
107
+ ["url"]=>
108
+ string(10) "http://..."
109
+ }
110
+ [1]=>
111
+ array(3) {
112
+ ["item"]=>
113
+ string(21) "猫缶A(関東限定)"
114
+ ["price"]=>
115
+ int(100)
116
+ ["url"]=>
117
+ string(10) "http://..."
118
+ }
119
+ }
120
+ ["カリカリ"]=>
121
+ array(1) {
122
+ [0]=>
123
+ array(3) {
124
+ ["item"]=>
125
+ string(7) "猫缶B"
126
+ ["price"]=>
127
+ int(150)
128
+ ["url"]=>
129
+ string(10) "http://..."
130
+ }
131
+ }
132
+ }
133
+ ["shop-c"]=>
134
+ array(2) {
135
+ ["猫缶"]=>
136
+ array(1) {
137
+ [0]=>
138
+ array(3) {
139
+ ["item"]=>
140
+ string(7) "猫缶A"
141
+ ["price"]=>
142
+ int(98)
143
+ ["url"]=>
144
+ string(10) "http://..."
145
+ }
146
+ }
147
+ ["カリカリ"]=>
148
+ array(1) {
149
+ [0]=>
150
+ array(3) {
151
+ ["item"]=>
152
+ string(7) "猫缶B"
153
+ ["price"]=>
154
+ int(110)
155
+ ["url"]=>
156
+ string(10) "http://..."
157
+ }
158
+ }
159
+ }
160
+ }
70
161
  ```