Page 1 of 1

extract values from array

Posted: Tuesday 15 August 2017 20:50
by ropske
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.

Re: extract values from array

Posted: Tuesday 15 August 2017 22:05
by ropske
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.

Re: extract values from array

Posted: Tuesday 15 August 2017 22:07
by ropske
Is there a 'way around' to inspect all received data?

Re: extract values from array

Posted: Tuesday 15 August 2017 22:09
by Egregius
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);

Re: extract values from array

Posted: Tuesday 15 August 2017 22:11
by ropske

Re: extract values from array

Posted: Tuesday 15 August 2017 22:11
by Egregius
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;

Re: extract values from array

Posted: Tuesday 15 August 2017 22:13
by Egregius
Why isn't that a comma separated list?
Let me look at it tomorrow, already did some crazy stuff with invalid json ;)

Re: extract values from array

Posted: Tuesday 15 August 2017 22:13
by ropske
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

Re: extract values from array

Posted: Wednesday 16 August 2017 6:57
by Egregius

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);

Re: extract values from array

Posted: Wednesday 16 August 2017 17:11
by ropske
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

Re: extract values from array

Posted: Wednesday 16 August 2017 17:13
by Egregius
You can't just print a array.
You could try it with (not tested):

lg(print_r($result,true));

Re: extract values from array

Posted: Wednesday 16 August 2017 17:30
by ropske
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

Re: extract values from array

Posted: Wednesday 16 August 2017 19:52
by Egregius
lg($result[3]['lat']); should do

Re: extract values from array

Posted: Wednesday 16 August 2017 21:53
by ropske
i tried this, but it is showing an empty line

Re: extract values from array

Posted: Thursday 17 August 2017 9:30
by ropske
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 ;)

Re: extract values from array

Posted: Thursday 17 August 2017 10:38
by Egregius
That's weird, but hey, if it works it's OK :)