0

I posted a question last night that turned out to not be the problem. Digging around, I have discovered that the below code is giving me a headche. I had this working, but now for some reason I get no output. When I var_dump the function that gives me the $finishmins value, it outputs everything correctly until the point where it has to search the array (as below). After this it shows NULL. I was originally using strpos to find out if it started with a zero, then stripped said zero to match to the array, but when it stopped working, I tried the below approach to reduce code.

The point of the code is to convert minutes in time to minutes in decimal notation. I.e. 1 minute = 02, thus 12:01 = 12.02.

$finishmins = '01';
$finishmins = $minarray[$finishmins];

$minarray = array(
00 => '00',
01 => '02',
02 => '03',
03 => '05',
04 => '07',
05 => '08',
06 => '10',
07 => '12',
08 => '13',
09 => '15',
10 => '17',
'18',
// Array continues to 59 => '98'
);

echo $finishmins;

I have pasted the complete code here: http://codepad.org/EUW3n7AB and still can't seem to find the problem.

3
  • Just saw this question, and I'm wondering if this is part of my problem: stackoverflow.com/questions/4750781/… Commented Jun 23, 2014 at 1:30
  • 1
    $minarray isn't defined in the scope of your function. Commented Jun 23, 2014 at 1:31
  • @Dr.Molle what would be my course of action? Revert back to stripping out the zeros using strpos and repost the code? Commented Jun 23, 2014 at 1:32

2 Answers 2

1

There are two issues here:

  1. Array indices behave differently between strings and numbers,
  2. Variable scope of $minarray.

$arr[01] and $arr['01'] are not the same thing, so you should be more explicit; in your case you can just leave the array numerically indexed, i.e.:

$minarray = array('00', '02', '03', '05', ...);

Then, you use an (int) cast on the given minutes:

$finishmins = $minarray[(int)$finishmins];

You can solve the second issue by passing the array as a function argument:

function finishtime($minarray, $finish) 

Then calling it like so:

echo finishtime($minarray, '12:01');
Sign up to request clarification or add additional context in comments.

5 Comments

So the reason I'm having issues is because I haven't passed the array into the function? Most of what you have said makes sense, just not sure if that's what you're meaning in regards to the array.
@BigJazzz Both issues I've mentioned cause your code to not give the expected result of "12.02".
Sorry, poor choice of words. That probably should have said "one of the reasons".
Yes indeed; this is because $minarray isn't defined inside the scope of finishtime().
Ok, I have it working here: codepad.org/QRqSvw2K but when I go back to my code it's still outputting 12.00. I have been var_dumping pretty much every step of the way, so I'll try building it from scratch again and see what happens. Cheers for this!
0

You have to use the keyword global in your function when referring to the $minarray variable:

function finishtime($finish) {
    global $minarray;
    $finishx = explode(':', $finish);
    $finishhours = $finishx[0];
    $finishmins = $finishx[1];
    $finishmins;
    var_dump($finishmins);
    $finishmins = $minarray[$finishmins];
    var_dump($finishmins);

    $finishtime = $finishhours . '.' . $finishmins;

    return $finishtime;
}

1 Comment

Thanks Lock. Was certainly part of the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.