0

I have an array that goes from '00:00:00' to '23:59:59', something like this ['00:00:00', '00:00:01' ... '23:59:59'].

I need to write a function to reduce the array length into n items but always keeping the first and the last element.

An example:

reduce_into($array, 3) -> ['00:00:00', '12:00:00', '23:59:59']

Note that the array must be "balanced", that means that when I reduce it into 3 elements it will return the first, the one in the middle and the last one.

1
  • Search google for "pie" and use the math you learned in grade school to cut it up into equally sized pieces. Or you could show us what you have tried so far. Commented Nov 1, 2015 at 1:32

1 Answer 1

2

Here's the code:

            function getnewarray($t,$i)
            {
                $newArr[0] = $t[0];
                $a = 1;
                $masterdivby = $divby = count($t) / ($i-1);
                $ni = $i-2;
                while($a <= $ni)
                    {
                    $newArr[$a] = $t[$divby];
                    $divby = $masterdivby + $divby;
                    $a++;   
                    }
                $newArr[$i]  = $t[count($t) - 1];   
                return $newArr;
            }
            ?>
Sign up to request clarification or add additional context in comments.

Comments

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.