【发布时间】:2015-01-06 17:56:45
【问题描述】:
我正在使用一个 for 循环,里面有一个 foreach 循环。在 foreach 循环中,我得到我需要的信息并将其回显到这样的字符串:19:00^-2,2° C。我想将时间(19:00)放在一个变量中,将温度(-2,2° C)放在另一个变量中,这样我就可以将它们用于the chart。但是我不知道如何将这两个拼接起来。这是目前的样子:
var yr = localStorage.yr.split('|');
time = yr[16].split('~');
temp = time.split('^');
lineChartData = {
labels: time,
datasets: [{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: temp
}]
}
使用此代码,我在temp = time.split('^'); 上得到Uncaught TypeError: undefined is not a function。 yr[16] 打印以下内容:
19:00^-2,2° C~20:00^-2° C~21:00^-2° C~22:00^-1,9° C~23:00^-1,6° C~00:00^-1,5° C~01:00^-1,3° C~02:00^-1,1° C~03:00^-0,9° C~04:00^-0,9° C~05:00^-1,1° C~06:00^-1,3° C~07:00^-1,6° C~
PHP 代码如下所示:
for($i = 0; $i < 13; $i++) {
$datetime = date('Y-m-d\TH', strtotime('+'.$i.' hour'));
$forecast_period = $forecast->xpath('(//product/time[contains(@datatype, "forecast")][contains(@from, "'.$datetime.':00:00Z")][contains(@to, "'.$datetime.':00:00Z")])');
foreach($forecast_period AS $period) {
$period_datetime = date('H:i', strtotime($period->attributes()->from));
$period_temperature = $period->location->temperature->attributes()->value;
$period_temperature_dewpoint = $period->location->dewpointTemperature->attributes()->value;
$period_temperature_unit = $period->location->temperature->attributes()->unit;
$period_wind_direction = $period->location->windDirection->attributes()->name;
$period_wind_direction_degrees = $period->location->windDirection->attributes()->deg;
$period_wind_speed = $period->location->windSpeed->attributes()->mps;
$period_fog = $period->location->fog->attributes()->percent;
$period_cloudiness = $period->location->cloudiness->attributes()->percent;
$period_cloudiness_low = $period->location->lowClouds->attributes()->percent;
$period_cloudiness_medium = $period->location->mediumClouds->attributes()->percent;
$period_cloudiness_high = $period->location->highClouds->attributes()->percent;
$period_pressure = $period->location->pressure->attributes()->value;
$period_pressure_unit = $period->location->pressure->attributes()->unit;
$period_humidity = $period->location->humidity->attributes()->value;
echo $period_datetime.'^'.temp($period_temperature, $period_temperature_unit, 'not-normal').'~';
}
}
我通过simplexml_load_file() 从YR's API 获取信息($forecast 是使用simplexml_load_file() 从 API 获取 XML 数据的人)。
话虽如此,我的问题是:如何将yr[16] 中的时间和温度放入两个变量中而不会出现任何错误,从而构建图表?
【问题讨论】:
-
你知道你可以简单地使用 JSON 吗?
-
time是一个数组。split()必须用于字符串,而不是数组。 -
您对
temp的期望是什么?time有13个时间和温度。 -
@Barmar 哦。我没想到。我将
temp = time.spl...更改为temp = yr[16].spl...并输出:["20:00", "-2° C~21:00", "-2° C~22:00", "-1,9° C~23:00", "-1,6° C~00:00", "-1,5° C~01:00", "-1,3° C~02:00", "-1,1° C~03:00", "-0,9° C~04:00", "-0,9° C~05:00", "-1,1° C~06:00", "-1,3° C~07:00", "-1,6° C~08:00", "-1,8° C~"]。现在我必须拆分数组的其余部分,但不是第一个。如何? :) -
@Barmar 是的。我知道。我想提取未来 13 小时的时间和温度并将其用于图表。
标签: javascript php arrays split