Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. <?php
  2. /**
  3. * Нужно написать код, который из массива выведет то что приведено ниже в комментарии.
  4. */
  5. $x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
  6.  
  7. function formatArray($x)
  8. {
  9. $foo = function ($x) use (&$foo) {
  10. if (prev($x)) {
  11. $acc[current($x)] = $foo($x);
  12. return $acc;
  13. } else {
  14. return '';
  15. }
  16. };
  17.  
  18. $result = [];
  19. $result[end($x)] = $foo($x);
  20.  
  21. return $result;
  22. }
  23.  
  24. echo '<pre>';
  25. print_r(formatArray($x));
  26. echo '</pre>';
  27.  
  28. /*
  29. print_r($x) - должен выводить это:
  30. Array
  31. (
  32. [h] => Array
  33. (
  34. [g] => Array
  35. (
  36. [f] => Array
  37. (
  38. [e] => Array
  39. (
  40. [d] => Array
  41. (
  42. [c] => Array
  43. (
  44. [b] => Array
  45. (
  46. [a] =>
  47. )
  48. )
  49. )
  50. )
  51. )
  52. )
  53. )
  54. );*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement