回答編集履歴
1
見直し
test
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
for-in文法をそのまま真似してかきたいのかもしれませんが、
|
2
|
+
|
3
|
+
郷に入っては郷に従え、
|
4
|
+
|
5
|
+
より便利な文法があれば使うべきだと思ったりもします。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
一定のデータ構造を先頭から末尾まで一つずつ順に処理するものだと察して、
|
10
|
+
|
11
|
+
だったらforeachでいいじゃないか、という考え方です。
|
12
|
+
|
13
|
+
何番目のデータを処理しているか、を意識して処理したい場合には、
|
14
|
+
|
15
|
+
引数が0,1,2って連続している前提で、[count](https://www.php.net/manual/ja/function.count.php)関数を使うのも手ではないかと。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
|
22
|
+
|
1
23
|
json文字列を[json_decode](https://www.php.net/manual/ja/function.json-decode.php)関数でデコードするときに、
|
2
24
|
|
3
25
|
オブジェクトと配列のどちらかを選択できます。
|
@@ -218,11 +240,11 @@
|
|
218
240
|
|
219
241
|
EOT;
|
220
242
|
|
221
|
-
$
|
243
|
+
$arr = json_decode($json, true);
|
222
244
|
|
223
245
|
$message = '';
|
224
246
|
|
225
|
-
foreach ($
|
247
|
+
foreach ($arr as $item){
|
226
248
|
|
227
249
|
$message .= $item['name'] . "\n```" . $item['gender'] . "```\n";
|
228
250
|
|
@@ -231,3 +253,115 @@
|
|
231
253
|
echo $message;
|
232
254
|
|
233
255
|
```
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
foreachじゃなくforにした事例:
|
260
|
+
|
261
|
+
```php
|
262
|
+
|
263
|
+
<?php
|
264
|
+
|
265
|
+
$json = <<<EOT
|
266
|
+
|
267
|
+
[
|
268
|
+
|
269
|
+
{
|
270
|
+
|
271
|
+
"age": 20,
|
272
|
+
|
273
|
+
"name": "Mccullough Blevins",
|
274
|
+
|
275
|
+
"gender": "male",
|
276
|
+
|
277
|
+
"company": "ARTWORLDS",
|
278
|
+
|
279
|
+
"email": "mcculloughblevins@artworlds.com",
|
280
|
+
|
281
|
+
"address": "257 Hillel Place, Yogaville, Tennessee, 5537"
|
282
|
+
|
283
|
+
},
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
"age": 25,
|
288
|
+
|
289
|
+
"name": "Casey Meadows",
|
290
|
+
|
291
|
+
"gender": "female",
|
292
|
+
|
293
|
+
"company": "TUBESYS",
|
294
|
+
|
295
|
+
"email": "caseymeadows@tubesys.com",
|
296
|
+
|
297
|
+
"address": "886 Cropsey Avenue, Kraemer, New Jersey, 7623"
|
298
|
+
|
299
|
+
},
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
"age": 27,
|
304
|
+
|
305
|
+
"name": "Lucinda Neal",
|
306
|
+
|
307
|
+
"gender": "female",
|
308
|
+
|
309
|
+
"company": "COMDOM",
|
310
|
+
|
311
|
+
"email": "lucindaneal@comdom.com",
|
312
|
+
|
313
|
+
"address": "802 Elton Street, Crown, Wyoming, 7838"
|
314
|
+
|
315
|
+
},
|
316
|
+
|
317
|
+
{
|
318
|
+
|
319
|
+
"age": 36,
|
320
|
+
|
321
|
+
"name": "Effie Martinez",
|
322
|
+
|
323
|
+
"gender": "female",
|
324
|
+
|
325
|
+
"company": "ZOINAGE",
|
326
|
+
|
327
|
+
"email": "effiemartinez@zoinage.com",
|
328
|
+
|
329
|
+
"address": "435 Tompkins Avenue, Rossmore, Washington, 5586"
|
330
|
+
|
331
|
+
},
|
332
|
+
|
333
|
+
{
|
334
|
+
|
335
|
+
"age": 28,
|
336
|
+
|
337
|
+
"name": "Rae Kirby",
|
338
|
+
|
339
|
+
"gender": "female",
|
340
|
+
|
341
|
+
"company": "ZIGGLES",
|
342
|
+
|
343
|
+
"email": "raekirby@ziggles.com",
|
344
|
+
|
345
|
+
"address": "719 Kimball Street, Muse, California, 4171"
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
]
|
350
|
+
|
351
|
+
EOT;
|
352
|
+
|
353
|
+
$arr = json_decode($json, true);
|
354
|
+
|
355
|
+
$message = '';
|
356
|
+
|
357
|
+
$cnt = count($arr);
|
358
|
+
|
359
|
+
for ($i = 0; $i < $cnt; $i++){
|
360
|
+
|
361
|
+
$message .= $arr[$i]['name'] . "\n```" . $arr[$i]['gender'] . "```\n";
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
echo $message;
|
366
|
+
|
367
|
+
```
|