I have struggled with this for a long time now. I have lost count of all the forum posts I have read and tried. Now I have reached the point where I have to turn to the experts. There are lots of questions and answers about arrays, but I have not been able to find one quite like mine. Or I have misunderstood them. If this is a duplicate question I apologize! Please point me in the right direction.
I want to dynamically feed a Google chart. Data comes from a web service, in json. I decode this to a php array. I am able to get data from the first ([SampleDate]) and second ([ReportedValue]) "level" and get them into a new array to feed the chart. But, I need to find other values corresponding to the other second level keys further down.
My first idea was to search (search parameter will come from a link from another page. But I will hopefully solve that later on.) for the right value (i.e [PropertyCode] => Ca), when found present the value for the corresponding key ([ReportedValue] => 2,15) and loop this to get all values (together with [Sampledate], for the chart). But I'm sure there are a lot of other ways to do what I am trying to do. The problem is I don't know how to proceed. If it's any help the complete code can be tested at PhpFiddle
I hope that you understand my question and are able to help me out.
Thanks in advance!
$stuff = json_decode($json, true);
print "<pre>";
print_r ($stuff);
$datum = array();
foreach ($stuff as $value){
$date = $value['SampleDate'];
preg_match('~(\d+)\d{3}((?:\+|-)\d+)~', $date, $match);
$dt = new DateTime('@' . $match[1]);
$dt = new DateTime($dt->format('Y-m-d H:i:s') , new DateTimeZone('UTC'));
$dt->setTimezone(new DateTimeZone('Europe/Stockholm'));
$dates = $dt->format('Y');
$valueChange = $value['ObservationValues'][0]['ReportedValue'];
$number = str_replace(',', '.', $valueChange);
$datum[] = array("SampleDate" =>$dates, "ReportedValue" =>$number);
}
//counting the length of the array
$countArrayLength = count($datum);
/*print "<pre>";
print_r ($datum);
print "</pre>";*/
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', '/5cm');
data.addRows([
<?php
for($i=0;$i<$countArrayLength;$i++){
echo "['" . $datum[$i]['SampleDate'] . "'," . $datum[$i]['ReportedValue'] . "],";
}
?>
]);
var options = {
title: 'Absorbans filtrerat (420 nm)',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
</script>
<div class="grid-container">
<div class="grid-100 grid-parent">
<div id="curve_chart" style="width: 250px; height: auto"></div>
</div>
</div>
Piece of array:
Array
(
[0] => Array
(
[HasSampleComment] =>
[IndexValues] => Array
(
)
[IsBiologicalSample] =>
[MaxDepth] => 0,5
[MinDepth] => 0,5
[NumSubSamples] =>
[ObservationValues] => Array
(
[0] => Array
(
[AccessRestriction] => 0
[IsComplexType] =>
[IsQualitative] =>
[ObservationSetId] => 249891
[ObservedPropertyId] => 6646553
[PropertyAbbrevName] => Abs_F 420
[PropertyAuthorityCode] => Default
[PropertyCode] => Abs_F420
[PropertyName] => Absorbans filtrerat (420 nm)
[ReportedValue] => 0,036
[TaxonSwedishName] =>
[UnitOfMeasureId] => 35
[UnitOfMeasureName] => /5cm
[ValueRepresentationTypeName] => NumericProperty
)
[1] => Array
(
[AccessRestriction] => 0
[IsComplexType] =>
[IsQualitative] =>
[ObservationSetId] => 249891
[ObservedPropertyId] => 6646552
[PropertyAbbrevName] => Abs_F 436
[PropertyAuthorityCode] => Default
[PropertyCode] => Abs_436m
[PropertyName] => Absorbans filtrerat (436 nm)
[ReportedValue] => 0,027
[TaxonSwedishName] =>
[UnitOfMeasureId] => 35
[UnitOfMeasureName] => /5cm
[ValueRepresentationTypeName] => NumericProperty
)
[2] => Array
(
...
)
)
[ObservedPropertyList] => Array
(
)
[ProductCode] => Vnk
[ProductName] => Vattenkemi
[RelativeLocationType] => 10
[SampleCommentFirst100] =>
[SampleCoordinate] =>
[SampleCoordinateSRS] =>
[SampleDate] => /Date(1408572000000+0200)/
[SampleId] => 249891
[SampleIsSensitive] =>
[SampleLocationWkt] =>
[SampleMediumTypeName] => Sjö
[SampleMetadataList] => Array
(
)
[SampleStatus] => Authorized
[SiteCoordinate] =>
[SiteEUId] => SE619750-135450
[SiteId] => 1314
[SiteName] => Västra Ringsjön
[SiteType] => Sjö
[SourceSampleId] => LIMS-Sample_97162
[StudyName] => Skåne län
[SubProgramName] => RMÖ
[TaxonGroupCalculationValues] =>
[UserIsAuthorized] =>
[WaterZone] =>
)
[1] => Array
(
[HasSampleComment] =>
[IndexValues] => Array
(
)
[IsBiologicalSample] =>
[MaxDepth] => 0,5
[MinDepth] => 0,5
[NumSubSamples] =>
[ObservationValues] => Array
(
[0] => Array
(
[AccessRestriction] => 0
[IsComplexType] =>
[IsQualitative] =>
[ObservationSetId] => 335161
[ObservedPropertyId] => 8160455
[PropertyAbbrevName] => Abs_F 420
[PropertyAuthorityCode] => Default
[PropertyCode] => Abs_F420
[PropertyName] => Absorbans filtrerat (420 nm)
[ReportedValue] => 0,046
[TaxonSwedishName] =>
[UnitOfMeasureId] => 35
[UnitOfMeasureName] => /5cm
[ValueRepresentationTypeName] => NumericProperty
)
[1] => Array
(......
Edit: I kept going at it and now I have an array that looks like this:
Array
(
[0] => Array
(
[0] => 2013
[1] => 2.15
)
[1] => Array
(
[2] => 2014
[3] => 2.21
)
[2] => Array
(
[4] => 2011
[5] => 1.933
)
[3] => Array
(
[6] => 2012
[7] => 2.033
)
[4] => Array
(
[8] => 2015
[9] => 2.20
)
)
For it to work with Google Charts I need to name the keys like this:
Array
(
[0] => Array
(
[SampleDate] => 2013
[ReportedValue] => 2.15
)
[1] => Array
(
[SampleDate] => 2014
[ReportedValue] => 2.21
)... and so on
Yet again I've spent many hours and tried everything I found even slightly in the same ballpark. With no luck. Please help!