我正在编写一个函数来从数组中获取值datetime值并将其转换为小时和分钟格式,如0010.我想使用它来稍后从数组中提取其他值,因为这是一个关联数组
function getData($lengthArray, $csvdat) { # passing down the values of the array and the length of the array
$newData = array(
'nightTime1',
'nightTime2',
'dayTime1',
'peakTime',
'dayTime2',
'winterNightTime1',
'winterNightTime2',
'winterDayTime1',
'winterDayTime2',
'winterPeakTime');
for ($count = 0; $count <= $lengthArray; $count++ ) { #looping to the length of the array
$arrayTimestamp = $csvdat[$count]['timestamp'];
$comparator = date('Hs', strtotime($arrayTimestamp)); #extracting the time format
$nightTime1 = 0759;
$dayTime1 = 1659;
$peakTime = 1859;
$dayTime2 = 2259;
$nightTime2 = 2359;
if ( $comparator == $nightTime1 ) {
if (empty($newData['nightTime1'])) {
$newData['nightTime1'] = $nightTime;
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan1'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan2'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan3'];
}
else if (!empty($newData)) {
}
}
}
return $newData;
}
getData($lengthData , $csvdata);
var_dump($newData);
我现在面临的问题是我在行
处得到一个未定义的偏移错误$arrayTimestamp = $csvdat[$count]['timestamp'];
和$newData
的未定义变量。我不是很擅长php,所以请指教。
以下是var_dump
数组
csvdat
array (size=118061)
0 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5972' (length=4)
'curr_property_cost' => string '62' (length=2)
'day_property' => string '19' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2898' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '9' (length=1)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2189' (length=4)
'curr_chan2' => string '2898' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '7' (length=1)
'day_chan2' => string '9' (length=1)
'day_chan3' => string '2' (length=1)
1 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '37' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2141' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '16' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2141' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '14' (length=2)
'day_chan2' => string '16' (length=2)
'day_chan3' => string '5' (length=1)
2 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '54' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2157' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '23' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2157' (length=4)
'curr_chan2' => string '2157' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '21' (length=2)
'day_chan2' => string '23' (length=2)
'day_chan3' => string '8' (length=1)
3 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5183' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '71' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2125' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '31' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2125' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '28' (length=2)
'day_chan2' => string '31' (length=2)
'day_chan3' => string '11' (length=2)
答案 0 :(得分:1)
尝试
for ($count = 0; $count < $lengthArray; $count++ )
而不是
for ($count = 0; $count <= $lengthArray; $count++ )
答案 1 :(得分:0)
如果并且只有当您100%确定$lengthArray
是$csvdat
数组的大小时,那么您的错误是由此行引起的:
for ($count = 0; $count <= $lengthArray; $count++ ) {
应该是
for ($count = 0; $count < $lengthArray; $count++ ) {
如果$count
低于$lengthArray
或与3
相同,那么你现在正在打破循环。请注意,数组中的最后一个键是4
,其长度为var_dump($newData)
。
现在关于未定义的变量:$ newData 。
您正在尝试getData
未定义。它存在于$newData = getData($lengthData , $csvdata);
var_dump($newData);
函数内部,但不在其外部。也许你想这样做:
{{1}}
答案 2 :(得分:0)
应该 $ count&lt; for循环中的$ lengthArray
for ($count = 0; $count < $lengthArray; $count++ ) {
}