fork(2) download
  1. <?php
  2.  
  3. class SpredArrayClass
  4. {
  5.  
  6. var $array = [1,2,3,4,5,6,7,8,9,9,10,11,12,13,14,15];
  7.  
  8. function spreadOutArray(array $array = [], $targetOutputLength = 10) {
  9.  
  10. $array = $this->array;
  11.  
  12. $originalArrayLength = count($array);
  13.  
  14. if ($originalArrayLength == 0) {
  15. return false;
  16. }
  17.  
  18. if ($originalArrayLength <= $targetOutputLength) {
  19. return $array;
  20. }
  21.  
  22. $output = [];
  23. $interval = round($originalArrayLength / $targetOutputLength);
  24.  
  25. // JUST KEEP LOOPING & PUSHING TILL SO LONG AS $index IS GREATER THAN 0;
  26. for($index = $originalArrayLength - 1; $index>=0; $index -= $interval) {
  27. $output[] = $array[$index];
  28. }
  29. return array_reverse($output);
  30. }
  31. }
  32.  
  33. $spredArrayClass = new SpredArrayClass;
  34. var_dump($spredArrayClass->spreadOutArray());
Success #stdin #stdout 0s 52488KB
stdin
Standard input is empty
stdout
array(8) {
  [0]=>
  int(2)
  [1]=>
  int(4)
  [2]=>
  int(6)
  [3]=>
  int(8)
  [4]=>
  int(9)
  [5]=>
  int(11)
  [6]=>
  int(13)
  [7]=>
  int(15)
}