回答編集履歴

1

答え

2021/08/12 13:48

投稿

hentaiman
hentaiman

スコア6426

test CHANGED
@@ -75,3 +75,65 @@
75
75
  - ミスる訳ないと決めつけて基礎的な記述をしている個所のデバッグを怠るのはNGです
76
76
 
77
77
  - 把握している純粋な情報に従ってデバッグしましょう(簡単な箇所でミスる訳ないという思い込みがあるからデバッグ対象から漏れる)
78
+
79
+
80
+
81
+ # 追記
82
+
83
+ 正しいdecodeと、誤ったdecode
84
+
85
+ ※質問者がうまくいかない理由が分かりやすくなるように
86
+
87
+ ```php
88
+
89
+ $json = '
90
+
91
+ [{"id":1,"date":"2021-06-30T15:00:00.000Z","name":"立花","attend":false},
92
+
93
+ {"id":2,"date":"2021-07-01T15:00:00.000Z","name":"橘","attend":true},
94
+
95
+ {"id":3,"date":"2021-07-02T15:00:00.000Z","name":"柳","attend":false},
96
+
97
+ {"id":4,"date":"2021-07-03T15:00:00.000Z","name":"桔梗","attend":true},
98
+
99
+ {"id":5,"date":"2021-07-04T15:00:00.000Z","name":"楓","attend":false}]';
100
+
101
+
102
+
103
+ $decode = json_decode($json, true);
104
+
105
+ echo $decode->date;
106
+
107
+ > PHP Warning: Attempt to read property "date" on array in php shell code on line 1
108
+
109
+
110
+
111
+ echo $decode['date'];
112
+
113
+ > PHP Warning: Undefined array key "date" in php shell code on line 1
114
+
115
+
116
+
117
+ echo $decode[0]['date'];
118
+
119
+ > 2021-06-30T15:00:00.000Z
120
+
121
+ $date = $decode[0]['date'];
122
+
123
+ echo $date != "" ? 'valid' : 'invalid';
124
+
125
+ > valid
126
+
127
+
128
+
129
+ $date = $decode->date;
130
+
131
+ > PHP Warning: Attempt to read property "date" on array in php shell code on line 1
132
+
133
+
134
+
135
+ echo $date != "" ? 'valid' : 'invalid';
136
+
137
+ >invalid
138
+
139
+ ```