extract values from array

Post Reply
ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

extract values from array

Post by ropske » Tuesday 15 August 2017 20:50

Hi,
i would love to extract all values, the result what i receive now is:

{"time":1502822744114809900,"lat":48.278868,"lon":3.704626,"alt":0,"pol":0,"mds":9412,"mcg":65} {"time":1502822743755137300,"lat":48.74463,"lon":12.346999,"alt":0,"pol":0,"mds":14795,"mcg":53} {"time":1502822743719286800,"lat":48.76882,"lon":12.345279,"alt":0,"pol":0,"mds":7864,"mcg":78} {"time":1502822743574110500,"lat":48.751461,"lon":12.291977,"alt":0,"pol":0,"mds":14299,"mcg":84} {"time":1502822743531312600,"lat":46.732674,"lon":12.635411,"alt":0,"pol":0,"mds":8500,"mcg":98} {"time":1502822743430266600,"lat":46.787249,"lon":12.645081,"alt":0,"pol":0,"mds":10899,"mcg":126} {"time":1502822743361762600,"lat":46.75697,"lon":12.664295,"alt":0,"pol":0,"mds":8831,"mcg":135} {"time":1502822743317798700,"lat":50.373524,"lon":5.757353,"alt":0,"pol":0,"mds":7804,"mcg":61} {"time":1502822743103383600,"lat":50.359257,"lon":5.767865,"alt":0,"pol":0,"mds":11733,"mcg":248} {"time":1502822743075182600,"lat":48.814166,"lon":12.446773,"alt":0,"pol":0,"mds":14876,"mcg":119}

I want to have every lat and lon coordinate and time value

i already tried with:

Code: Select all

$bliksems=file_get_contents('http://blablabla');
foreach($bliksems as $bliksem){
$lat=$bliksems['lat'];
lg($lat);
}
but this results in an empty value
the answer i'm getting from $bliksems is not a real json it seems (i guess because the layout is different)

can someone help me or just guide me in the correct way, thank you.

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Tuesday 15 August 2017 22:05

You are correct, that is a real JSON with the [] brackets and with a comma between each object.

But the problem is, i cant change anything from the data i'm receiving.

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Tuesday 15 August 2017 22:07

Is there a 'way around' to inspect all received data?

User avatar
Egregius
Posts: 2738
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: extract values from array

Post by Egregius » Tuesday 15 August 2017 22:09

Can you post the complete urL?
@ropske: you should at least have a json_decode in your code.

Code: Select all

$bliksems=json_decode(file_get_contents('http://...'),true);

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Tuesday 15 August 2017 22:11


User avatar
Egregius
Posts: 2738
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: extract values from array

Post by Egregius » Tuesday 15 August 2017 22:11

ropske wrote:
Tuesday 15 August 2017 22:07
Is there a 'way around' to inspect all received data?
Yes
$bliksems=file_get_contents...
echo $bliksems;

User avatar
Egregius
Posts: 2738
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: extract values from array

Post by Egregius » Tuesday 15 August 2017 22:13

Why isn't that a comma separated list?
Let me look at it tomorrow, already did some crazy stuff with invalid json ;)

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Tuesday 15 August 2017 22:13

I already asked the people at Blitzortung why they can not output the data as a real JSON

still waiting for an answer from them

User avatar
Egregius
Posts: 2738
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: extract values from array

Post by Egregius » Wednesday 16 August 2017 6:57

Code: Select all

//Get the data
$bliksems=file_get_contents('http://data2.blitzortung.org/Data/Protected/last_strikes.php?number=10&west=-20&east=30&north=60&south=30&sig=0');
//Split the string at } and put it in an array
$bliksems=explode('}',$bliksems);
//remove the last item of the array because it's empty
array_pop($bliksems);
foreach($bliksems as $bliksem){
	//remove the first characters until {
	$data=substr(strstr($bliksem,'{'),1);
	//split the new string in chunks with : and , as delimiters
	$chunks=array_chunk(preg_split('/(:|,)/',$data),2);
	//combine the chunks and add them to a new array
	$result[]=array_combine(array_column($chunks,0),array_column($chunks,1));
}
//Done
print_r($result);

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Wednesday 16 August 2017 17:11

when i change the

Code: Select all

print_r($result) into lg($result)
i only get text: Array

i'm checking it with pass2php ;)
so i check the domoticz log file

User avatar
Egregius
Posts: 2738
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: extract values from array

Post by Egregius » Wednesday 16 August 2017 17:13

You can't just print a array.
You could try it with (not tested):

lg(print_r($result,true));

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Wednesday 16 August 2017 17:30

Correct, that's showing the complete array

and what if i want element 'lat' from array 3 ?
lg(print_r($result[3]->lat,true));
or is it lg(print_r($result[3]['lat'],true));

?
Thank you

User avatar
Egregius
Posts: 2738
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: extract values from array

Post by Egregius » Wednesday 16 August 2017 19:52

lg($result[3]['lat']); should do

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Wednesday 16 August 2017 21:53

i tried this, but it is showing an empty line

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: extract values from array

Post by ropske » Thursday 17 August 2017 9:30

found the solution, to access my values in the array i needed to do this:

lg($result[0]['"lat"']);

first single ' => to access the element
but then double ' => because the element is a set in the array in "

thank you ;)

User avatar
Egregius
Posts: 2738
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: extract values from array

Post by Egregius » Thursday 17 August 2017 10:38

That's weird, but hey, if it works it's OK :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest