回答編集履歴

1

chousei

2021/11/10 02:43

投稿

yambejp
yambejp

スコア114843

test CHANGED
@@ -17,3 +17,51 @@
17
17
  print_r($fruits);
18
18
 
19
19
  ```
20
+
21
+ # $fruitsを調整したもの
22
+
23
+ ```PHP
24
+
25
+ $shoppingBasket = [
26
+
27
+ ['fruit' => 'apple', 'price' =>150],
28
+
29
+ ['fruit' => 'orange', 'price' =>150],
30
+
31
+ ['fruit' => 'grape', 'price' =>300],
32
+
33
+ ['fruit' => 'strawberry', 'price' =>300],
34
+
35
+ ['fruit' => 'pineapple', 'price' =>500],
36
+
37
+ ];
38
+
39
+ $fruits = [
40
+
41
+ 'apple'=>0,
42
+
43
+ 'orange'=>0,
44
+
45
+ 'grape'=>0,
46
+
47
+ 'strawberry'=>0,
48
+
49
+ 'pineapple'=>0,
50
+
51
+ ];
52
+
53
+ foreach(array_keys($fruits) as $fruit){
54
+
55
+ $fruits[$fruit]=count(array_filter($shoppingBasket,function($x) use($fruit){
56
+
57
+ return $x["fruit"]==$fruit;
58
+
59
+ }));
60
+
61
+ }
62
+
63
+ print_r($fruits);
64
+
65
+
66
+
67
+ ```