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

回答編集履歴

1

サンプルコードを追加

2020/04/08 13:17

投稿

退会済みユーザー
answer CHANGED
@@ -1,2 +1,76 @@
1
1
  resultsってArrayで返ってくるから、
2
- foreach()ループを回すなりしたらいいんじゃないかとー。
2
+ foreach()ループを回すなりしたらいいんじゃないかとー。
3
+
4
+ ```php
5
+ <?php
6
+
7
+ $json = <<<EOT
8
+ {
9
+ "results" : [
10
+ {
11
+ "address_components" : [
12
+ {
13
+ "long_name" : "1600",
14
+ "short_name" : "1600",
15
+ "types" : [ "street_number" ]
16
+ },
17
+ {
18
+ "long_name" : "Amphitheatre Pkwy",
19
+ "short_name" : "Amphitheatre Pkwy",
20
+ "types" : [ "route" ]
21
+ },
22
+ {
23
+ "long_name" : "Mountain View",
24
+ "short_name" : "Mountain View",
25
+ "types" : [ "locality", "political" ]
26
+ },
27
+ {
28
+ "long_name" : "Santa Clara County",
29
+ "short_name" : "Santa Clara County",
30
+ "types" : [ "administrative_area_level_2", "political" ]
31
+ },
32
+ {
33
+ "long_name" : "California",
34
+ "short_name" : "CA",
35
+ "types" : [ "administrative_area_level_1", "political" ]
36
+ },
37
+ {
38
+ "long_name" : "United States",
39
+ "short_name" : "US",
40
+ "types" : [ "country", "political" ]
41
+ },
42
+ {
43
+ "long_name" : "94043",
44
+ "short_name" : "94043",
45
+ "types" : [ "postal_code" ]
46
+ }
47
+ ],
48
+ "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
49
+ "geometry" : {
50
+ "location" : {
51
+ "lat" : 37.4224764,
52
+ "lng" : -122.0842499
53
+ },
54
+ "location_type" : "ROOFTOP",
55
+ "viewport" : {
56
+ "northeast" : {
57
+ "lat" : 37.4238253802915,
58
+ "lng" : -122.0829009197085
59
+ },
60
+ "southwest" : {
61
+ "lat" : 37.4211274197085,
62
+ "lng" : -122.0855988802915
63
+ }
64
+ }
65
+ },
66
+ "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
67
+ "types" : [ "street_address" ]
68
+ }
69
+ ],
70
+ "status" : "OK"
71
+ }
72
+ EOT;
73
+ foreach (json_decode($json)->results as $result) {
74
+ echo 'lat:' . $result->geometry->location->lat . ', lng:' . $result->geometry->location->lng . PHP_EOL;
75
+ }
76
+ ```