fork download
  1. <?php
  2.  
  3. function make_regexp($x) {
  4. $keys = explode(':', $x);
  5. $last = "(?P<" . array_pop($keys) . ">.+)";
  6. array_walk($keys, function(&$v, $k) { $v = "(?P<$v>[^:]+)";});
  7. array_push($keys, $last);
  8. return '#^' . implode(':', $keys) . '$#';
  9. }
  10.  
  11. function slice($a, $key) {
  12. return array_filter($a, function($k)use($key) {return in_array($k, explode(':', $key));}, ARRAY_FILTER_USE_KEY);
  13. }
  14.  
  15. function f($a) {
  16. $keys = implode(':', array_keys($a));
  17. if(preg_match(make_regexp('a:b:lst'), $keys, $matches)) {
  18. return array_merge(slice($a, $matches['b']), f(slice($a, $matches['lst'])));
  19. }
  20.  
  21. if(preg_match(make_regexp('a:b'), $keys, $matches)) {
  22. return [$a[$matches['b']]];
  23. }
  24.  
  25. return [];
  26. }
  27.  
  28. echo implode(' ', f(['Хорошо','живёт','на свете','Винни-пух']));
  29.  
Success #stdin #stdout 0.02s 24548KB
stdin
Standard input is empty
stdout
живёт Винни-пух