回答編集履歴
1
追記
test
CHANGED
@@ -38,6 +38,14 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
+
また、これは致命的では無く既存コードやプロジェクトとの兼ね合いもあるとは思いますが、
|
42
|
+
|
43
|
+
PHP5.4以降のコードを書く場合、配列の定義はarray()よりも[](短縮構文)の方がすっきりします。
|
44
|
+
|
45
|
+
(array()で書いていると、教科書が相当古いとか、元になっているコードが相当古いという印象を受けるので、特に理由が無ければ[]で記述されることをお勧めします。)
|
46
|
+
|
47
|
+
|
48
|
+
|
41
49
|
|
42
50
|
|
43
51
|
原因
|
@@ -326,11 +334,215 @@
|
|
326
334
|
|
327
335
|
|
328
336
|
|
329
|
-
|
337
|
+
初期データで同じように書いた場合はこんな感じ
|
330
|
-
|
338
|
+
|
339
|
+
|
340
|
+
|
331
|
-
```PHP
|
341
|
+
```PHP
|
342
|
+
|
332
|
-
|
343
|
+
$db['db']['station'][] = [
|
344
|
+
|
333
|
-
|
345
|
+
'name' => '三ノ宮',
|
346
|
+
|
347
|
+
'prefecture' => '兵庫県',
|
348
|
+
|
349
|
+
'line' => 'JR神戸線',
|
350
|
+
|
351
|
+
'x' => 135.1952558,
|
352
|
+
|
353
|
+
'y' => 34.6945454,
|
354
|
+
|
355
|
+
'postal' => '6770039',
|
356
|
+
|
357
|
+
'distance' => '100m',
|
358
|
+
|
359
|
+
'prev' => '',
|
360
|
+
|
361
|
+
'next' => ''
|
362
|
+
|
363
|
+
];
|
364
|
+
|
365
|
+
$db['db']['station'][] = [
|
366
|
+
|
367
|
+
'name' => '神戸三宮',
|
368
|
+
|
369
|
+
'prefecture' => '兵庫県',
|
370
|
+
|
371
|
+
'line' => '阪神',
|
372
|
+
|
373
|
+
'x' => 135.1952558,
|
374
|
+
|
375
|
+
'y' => 34.6945454,
|
376
|
+
|
377
|
+
'postal' => '6770039',
|
378
|
+
|
379
|
+
'distance' => '100m',
|
380
|
+
|
381
|
+
'prev' => '',
|
382
|
+
|
383
|
+
'next' => ''
|
384
|
+
|
385
|
+
];
|
386
|
+
|
387
|
+
//以下駅の数だけ続く
|
388
|
+
|
389
|
+
```
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
$arrに適用する場合の全体はこんな感じ
|
394
|
+
|
395
|
+
*各ifは条件を満たすのがめんどくさかったので条件を変更しています。
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
```PHP
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
<?php
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
header('Content-Type: application/json; charset=UTF-8');
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
// データリソース(モックアップ)
|
414
|
+
|
415
|
+
$db['db'] = array(
|
416
|
+
|
417
|
+
'station' => array(
|
418
|
+
|
419
|
+
array (
|
420
|
+
|
421
|
+
'name' => '三ノ宮',
|
422
|
+
|
423
|
+
'prefecture' => '兵庫県',
|
424
|
+
|
425
|
+
'line' => 'JR神戸線',
|
426
|
+
|
427
|
+
'x' => 135.1952558,
|
428
|
+
|
429
|
+
'y' => 34.6945454,
|
430
|
+
|
431
|
+
'postal' => '6770039',
|
432
|
+
|
433
|
+
'distance' => '100m',
|
434
|
+
|
435
|
+
'prev' => '',
|
436
|
+
|
437
|
+
'next' => ''
|
438
|
+
|
439
|
+
),
|
440
|
+
|
441
|
+
array (
|
442
|
+
|
443
|
+
'name' => '神戸三宮',
|
444
|
+
|
445
|
+
'prefecture' => '兵庫県',
|
446
|
+
|
447
|
+
'line' => '阪神',
|
448
|
+
|
449
|
+
'x' => 135.1952558,
|
450
|
+
|
451
|
+
'y' => 34.6945454,
|
452
|
+
|
453
|
+
'postal' => '6770039',
|
454
|
+
|
455
|
+
'distance' => '100m',
|
456
|
+
|
457
|
+
'prev' => '',
|
458
|
+
|
459
|
+
'next' => ''
|
460
|
+
|
461
|
+
),
|
462
|
+
|
463
|
+
array (
|
464
|
+
|
465
|
+
'name' => '東京',
|
466
|
+
|
467
|
+
'prefecture' => '東京都',
|
468
|
+
|
469
|
+
'line' => 'JR山手線',
|
470
|
+
|
471
|
+
'x' => 139.7673068,
|
472
|
+
|
473
|
+
'y' => 35.6809591,
|
474
|
+
|
475
|
+
'postal' => '6770039',
|
476
|
+
|
477
|
+
'distance' => '150m',
|
478
|
+
|
479
|
+
'prev' => '',
|
480
|
+
|
481
|
+
'next' => ''
|
482
|
+
|
483
|
+
),
|
484
|
+
|
485
|
+
array (
|
486
|
+
|
487
|
+
'name' => '神田',
|
488
|
+
|
489
|
+
'prefecture' => '東京都',
|
490
|
+
|
491
|
+
'line' => 'JR山手線',
|
492
|
+
|
493
|
+
'x' => 139.7709,
|
494
|
+
|
495
|
+
'y' => 35.6918,
|
496
|
+
|
497
|
+
'postal' => '6770039',
|
498
|
+
|
499
|
+
'distance' => '150m',
|
500
|
+
|
501
|
+
'prev' => '',
|
502
|
+
|
503
|
+
'next' => ''
|
504
|
+
|
505
|
+
),
|
506
|
+
|
507
|
+
),
|
508
|
+
|
509
|
+
);
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
$arr = [];
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
if(true) { // methodでGET送信
|
518
|
+
|
519
|
+
$getX = 0;//htmlspecialchars($_GET['x']); // xでGET送信
|
520
|
+
|
521
|
+
$getY = 0;//htmlspecialchars($_GET['y']); // yでGET送信
|
522
|
+
|
523
|
+
foreach ($db as $val) {
|
524
|
+
|
525
|
+
foreach ($val['station'] as $station_detail) {
|
526
|
+
|
527
|
+
$name = $station_detail['name'];
|
528
|
+
|
529
|
+
$prefecture = $station_detail['prefecture'];
|
530
|
+
|
531
|
+
$line = $station_detail['line'];
|
532
|
+
|
533
|
+
$longitude = floatval($station_detail['x']);
|
534
|
+
|
535
|
+
$latitude = floatval($station_detail['y']);
|
536
|
+
|
537
|
+
$postal = $station_detail['postal'];
|
538
|
+
|
539
|
+
$distance = $station_detail['distance'];
|
540
|
+
|
541
|
+
$prev = $station_detail['prev'];
|
542
|
+
|
543
|
+
$next = $station_detail['next'];
|
544
|
+
|
545
|
+
if(true){
|
334
546
|
|
335
547
|
$arr['response']['station'][] = array(
|
336
548
|
|
@@ -352,167 +564,33 @@
|
|
352
564
|
|
353
565
|
'next' => $next);
|
354
566
|
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
```
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
// データリソース(モックアップ)
|
384
|
-
|
385
|
-
$db['db'] = array(
|
386
|
-
|
387
|
-
'station' => array(
|
388
|
-
|
389
|
-
array (
|
390
|
-
|
391
|
-
'name' => '三ノ宮',
|
392
|
-
|
393
|
-
'prefecture' => '兵庫県',
|
394
|
-
|
395
|
-
'line' => 'JR神戸線',
|
396
|
-
|
397
|
-
'x' => 135.1952558,
|
398
|
-
|
399
|
-
'y' => 34.6945454,
|
400
|
-
|
401
|
-
'postal' => '6770039',
|
402
|
-
|
403
|
-
'distance' => '100m',
|
404
|
-
|
405
|
-
'prev' => '',
|
406
|
-
|
407
|
-
'next' => ''
|
408
|
-
|
409
|
-
),
|
410
|
-
|
411
|
-
array (
|
412
|
-
|
413
|
-
'name' => '神戸三宮',
|
414
|
-
|
415
|
-
'prefecture' => '兵庫県',
|
416
|
-
|
417
|
-
'line' => '阪神',
|
418
|
-
|
419
|
-
'x' => 135.1952558,
|
420
|
-
|
421
|
-
'y' => 34.6945454,
|
422
|
-
|
423
|
-
'postal' => '6770039',
|
424
|
-
|
425
|
-
'distance' => '100m',
|
426
|
-
|
427
|
-
'prev' => '',
|
428
|
-
|
429
|
-
'next' => ''
|
430
|
-
|
431
|
-
),
|
432
|
-
|
433
|
-
array (
|
434
|
-
|
435
|
-
'name' => '東京',
|
436
|
-
|
437
|
-
'prefecture' => '東京都',
|
438
|
-
|
439
|
-
'line' => 'JR山手線',
|
440
|
-
|
441
|
-
'x' => 139.7673068,
|
442
|
-
|
443
|
-
'y' => 35.6809591,
|
444
|
-
|
445
|
-
'postal' => '6770039',
|
446
|
-
|
447
|
-
'distance' => '150m',
|
448
|
-
|
449
|
-
'prev' => '',
|
450
|
-
|
451
|
-
'next' => ''
|
452
|
-
|
453
|
-
),
|
454
|
-
|
455
|
-
array (
|
456
|
-
|
457
|
-
'name' => '神田',
|
458
|
-
|
459
|
-
'prefecture' => '東京都',
|
460
|
-
|
461
|
-
'line' => 'JR山手線',
|
462
|
-
|
463
|
-
'x' => 139.7709,
|
464
|
-
|
465
|
-
'y' => 35.6918,
|
466
|
-
|
467
|
-
'postal' => '6770039',
|
468
|
-
|
469
|
-
'distance' => '150m',
|
470
|
-
|
471
|
-
'prev' => '',
|
472
|
-
|
473
|
-
'next' => ''
|
474
|
-
|
475
|
-
),
|
476
|
-
|
477
|
-
),
|
478
|
-
|
479
|
-
);
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
$arr = [];
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
if(true) { // methodでGET送信
|
488
|
-
|
489
|
-
$getX = 0;//htmlspecialchars($_GET['x']); // xでGET送信
|
490
|
-
|
491
|
-
$getY = 0;//htmlspecialchars($_GET['y']); // yでGET送信
|
492
|
-
|
493
|
-
foreach ($db as $val) {
|
494
|
-
|
495
|
-
foreach ($val['station'] as $station_detail) {
|
496
|
-
|
497
|
-
$name = $station_detail['name'];
|
498
|
-
|
499
|
-
$prefecture = $station_detail['prefecture'];
|
500
|
-
|
501
|
-
$line = $station_detail['line'];
|
502
|
-
|
503
|
-
$longitude = floatval($station_detail['x']);
|
504
|
-
|
505
|
-
$latitude = floatval($station_detail['y']);
|
506
|
-
|
507
|
-
$postal = $station_detail['postal'];
|
508
|
-
|
509
|
-
$distance = $station_detail['distance'];
|
510
|
-
|
511
|
-
$prev = $station_detail['prev'];
|
512
|
-
|
513
|
-
$next = $station_detail['next'];
|
514
|
-
|
515
|
-
if(true){
|
567
|
+
}
|
568
|
+
|
569
|
+
}
|
570
|
+
|
571
|
+
}
|
572
|
+
|
573
|
+
} else {
|
574
|
+
|
575
|
+
$arr['status'] = 'no request';
|
576
|
+
|
577
|
+
}
|
578
|
+
|
579
|
+
|
580
|
+
|
581
|
+
print json_encode($arr, JSON_PRETTY_PRINT);
|
582
|
+
|
583
|
+
```
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
ポイントはこの辺
|
588
|
+
|
589
|
+
|
590
|
+
|
591
|
+
```PHP
|
592
|
+
|
593
|
+
|
516
594
|
|
517
595
|
$arr['response']['station'][] = array(
|
518
596
|
|
@@ -534,20 +612,6 @@
|
|
534
612
|
|
535
613
|
'next' => $next);
|
536
614
|
|
537
|
-
|
615
|
+
|
538
|
-
|
539
|
-
|
616
|
+
|
540
|
-
|
541
|
-
}
|
542
|
-
|
543
|
-
} else {
|
544
|
-
|
545
|
-
$arr['status'] = 'no request';
|
546
|
-
|
547
|
-
}
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
print json_encode($arr, JSON_PRETTY_PRINT);
|
552
|
-
|
553
|
-
```
|
617
|
+
```
|