Advertisement
Guest User

PHP5 recursive yield

a guest
Apr 21st, 2017
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.57 KB | None | 0 0
  1. <?php
  2.  
  3. $theArray = [
  4.     'lorem' => [
  5.         'ipsum' => [
  6.             'dolor' => []
  7.         ],
  8.         'sit' => []
  9.     ],
  10.     'amet' => []
  11. ];
  12.  
  13. function expandArr($arr, $prefix = '') {
  14.     foreach ($arr as $key=>$value) {
  15.         echo '- yielding value ' . $prefix . $key . PHP_EOL;
  16.         yield $prefix . $key;
  17.         foreach (expandArr($value, $prefix . $key . '/') as $inner) {
  18.             echo '- passing thru yield ' . $inner . PHP_EOL;
  19.             yield $inner;
  20.         }
  21.     }
  22. }
  23.  
  24. foreach (expandArr($theArray) as $str) {
  25.     echo $str . PHP_EOL;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement