fork download
  1. <?php
  2.  
  3. $array = [
  4. 'name' => 'Allen Linatoc',
  5. 'profile' => [
  6. 'age' => 21,
  7. 'favourite_games' => [ 'Call of Duty', 'Titanfall', 'Far Cry' ]
  8. ]
  9. ];
  10.  
  11. foreach (iterate($array) as $item) {
  12. var_dump($item);
  13. };
  14.  
  15. function iterate($array)
  16. {
  17. foreach ($array as $item) {
  18. if (is_array($item)) {
  19. yield from iterate($item);
  20. } else {
  21. yield $item;
  22. }
  23. }
  24. }
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
string(13) "Allen Linatoc"
int(21)
string(12) "Call of Duty"
string(9) "Titanfall"
string(7) "Far Cry"