fork download
  1. <?php
  2.  
  3. $balls = [
  4. ["color" => "green", "cost" => 1, "weight" => 5],
  5. ["color" => "red", "cost" => 3, "weight" => 3],
  6. ["color" => "blue", "cost" => 5, "weight" => 2]
  7. ];
  8.  
  9.  
  10.  
  11.  
  12. function grabBalls($balls, $ballsTaken) {
  13. foreach($balls as $ball) {
  14. $ballsTaken[] = $ball["color"];
  15.  
  16. if (count($ballsTaken) < 5) {
  17. grabBalls($balls, $ballsTaken);
  18. } else {
  19. var_dump($ballsTaken);
  20.  
  21. }
  22.  
  23. return $ballsTaken;
  24. }
  25. }
  26.  
  27. $a = [];
  28. grabBalls($balls, $a);
  29.  
  30.  
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
array(5) {
  [0]=>
  string(5) "green"
  [1]=>
  string(5) "green"
  [2]=>
  string(5) "green"
  [3]=>
  string(5) "green"
  [4]=>
  string(5) "green"
}
array(0) {
}