fork(1) download
  1. <?php
  2.  
  3. //Sample application to find available values ​​in a merged collection:
  4.  
  5. function checkVacancies(array $positions) {
  6.  
  7. $available = [];
  8.  
  9. foreach ($positions as $position => $optionAvailable) {
  10.  
  11. $nextBox = false;
  12. $prevBox = false;
  13.  
  14. if (isset($positions[$position+1])) {
  15. $nextBox = $positions[$position+1];
  16. }
  17. if (isset($positions[$position-1])) {
  18. $prevBox = $positions[$position-1];
  19. }
  20.  
  21. if ($nextBox == false && $prevBox == false && $optionAvailable == false) {
  22. $available[] = $position;
  23. }
  24.  
  25. }
  26. return $available;
  27. }
  28. //false is returned position only if there is a real approximate value
  29.  
  30.  
  31. $positions = [
  32. true, //0
  33. false,
  34. false, //2
  35. false,
  36. true,
  37. false,
  38. false, //8
  39. false, //
  40. false
  41. ];
  42.  
  43. print_r(checkVacancies($positions));
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 2
    [1] => 6
    [2] => 7
    [3] => 8
)