0

I have a function that I am writing to take in values datetime values from an array and convert it into hours and minutes format like 0010. I want to use this to later extract other values from the array as this is an associative array

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

The problem I am facing now is that I get an Undefined offset error at the line

$arrayTimestamp = $csvdat[$count]['timestamp']; 

and an undefined variable for $newData. I am not very good at php so please advise.

Here is the var_dump of the csvdat array

 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) 
1
  • Would you please show us the content of $csvdat? Commented Feb 26, 2014 at 10:53

3 Answers 3

1

Try

for ($count = 0; $count < $lengthArray; $count++ ) 

instead of

for ($count = 0; $count <= $lengthArray; $count++ ) 
Sign up to request clarification or add additional context in comments.

Comments

0

If and only if you are 100% sure that $lengthArray is the size of the $csvdat array, then your error is caused by this line:

for ($count = 0; $count <= $lengthArray; $count++ ) { 

it should be

for ($count = 0; $count < $lengthArray; $count++ ) { 

Right now you're breaking the loop if the $count is lower OR the same as $lengthArray. Notice that last key in your array is 3 and its length is 4.


EDIT

Now about Undefined variable: $newData.

You're trying to var_dump($newData) which is undefined. It exists inside the getData function but not outside it. Maybe you wanted to do it like this:

$newData = getData($lengthData , $csvdata); var_dump($newData); 

2 Comments

thanks that did solve the problem for the offset. However the error still exists for undefined variable $newData
I found my mistake. Thanks, it was not what you pointed out though
0

It should be $count < $lengthArray in for loop

for ($count = 0; $count < $lengthArray; $count++ ) { } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.