我很久以来一直在努力解决这个问题。我已经阅读并尝试了所有论坛帖子。现在我已经达到了必须转向专家的程度。关于数组有很多问题和答案,但我找不到一个像我一样的问题和答案。或者我误解了他们。如果这是一个重复的问题,我道歉!请指出我正确的方向。
我想动态提供Google图表。数据来自json中的Web服务。我将其解码为php数组。 我能够从第一个([SampleDate])和第二个([ReportedValue])“级别”获取数据,并将它们放入一个新的数组中以提供图表。但是,我需要找到与其他二级键相对应的其他值。
我的第一个想法是搜索(搜索参数将来自另一个页面的链接。但我希望稍后会解决。)为正确的值(即[PropertyCode] => Ca ),当找到相应键的值时([ReportedValue] => 2,15 )并循环它以获取图表的所有值(与[Sampledate]一起)。但我确信还有很多其他方法可以做我想做的事情。问题是我不知道该怎么办。 如果有任何帮助,可以在PhpFiddle
测试完整的代码我希望你能理解我的问题并且能够帮助我。
提前致谢!
$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>
阵列:
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
(......
编辑: 我一直坚持下去,现在我有一个看起来像这样的数组:
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
)
)
要使用Google Charts,我需要命名这样的键:
Array
(
[0] => Array
(
[SampleDate] => 2013
[ReportedValue] => 2.15
)
[1] => Array
(
[SampleDate] => 2014
[ReportedValue] => 2.21
)... and so on
然而,我又花了很多时间尝试了我在同一个球场中发现的一切。没有运气。请帮忙!