I have tryed almost everything to get the data in observations in a foreach sentence in php but can get it to output any data.
I Want to get the observations rows listed so i can insert it into a MySQL DB
{
"version":"2.0","secret":"meraki","type":"DevicesSeen","data":{
"apMac":"88:15:44:84:4c:90","apFloors":[],"apTags":["","recently-added",""]
,"observations":[{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:43Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"94:9f:3e:06:c1:6c","seenEpoch":1495283083,"rssi":47,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}
]
}
}
https://jsonlint.com/ Your json code has syntax problem! one } was missing! Then go ahead with the other answers :)
<?php
$something = json_decode('{
"version":"2.0",
"secret":"meraki",
"type":"DevicesSeen",
"data":{
"apMac":"88:15:44:84:4c:90",
"apFloors":[],
"apTags":["","recently-added",""],
"observations":[{
"ipv4":null,
"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},
"seenTime":"2017-05-20T12:24:43Z",
"ssid":null,
"os":"Cisco/Linksys Router",
"clientMac":"94:9f:3e:06:c1:6c",
"seenEpoch":1495283083,
"rssi":47,
"ipv6":null,
"manufacturer":"Sonos"
},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}]
}
}');
foreach($something->data->observations[0] as $key => $val){
if($key === "location"){
echo '<hr><h4>here is the location object</h4>';
foreach($something->data->observations[0]->$key as $item => $data){
print_r(['item'=>$item,'data'=>$data]);
}
echo '<hr>';
}
print_r($something->data->observations[0]->$key);
foreach($something->data->observations[0]->$key as $item => $data){
if($item === '')
print_r(['item'=>$item,'data'=>$data]);
}
print_r(['key'=>$key,'val'=>$val]);
echo '<br>';
}
echo '<hr>';
print_r($something->data->observations[0]);
echo '<hr>';
print_r($something);
echo '<hr>';
exit;
<?php
// you have to use json_decode for sure
$something = json_decode('{
"version":"2.0","secret":"meraki","type":"DevicesSeen","data":{
"apMac":"88:15:44:84:4c:90","apFloors":[],"apTags":["","recently-added",""]
,"observations":[{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:43Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"94:9f:3e:06:c1:6c","seenEpoch":1495283083,"rssi":47,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}
]
}
}',true);
// first of all you miss an {
// use the second argument "true" to make it return an array
foreach ( $something['data']['observations'] as $item ){
$location_lng = $item['location']['lng'];
$location_lat = $item['location']['lat'];
echo $location_lng;
echo '<hr>';
echo $location_lat;
// Everything is good :)
}
?>