Example of an infinite loop array (recursion)

Source: Internet
Author: User
Example of an infinite loop array (recursion)

  1. /**

  2. * Author: GuoWangYunYan
  3. * QQ: 279861795
  4. * Date: 2011-6-23
  5. * Link: www.jbuxe.com
  6. */
  7. // Set the encoding
  8. Header ('content-type: text/html; charset = utf-8 ');
  9. // Abnormal, a five-dimensional array is used.
  10. $ A = array (
  11. 'Aaaaa' => array (
  12. 'Aaaaa' => array (
  13. '123 ',
  14. '123 ',
  15. '123'
  16. ),
  17. 'Bbbbbbbb' => array (
  18. '123 ',
  19. '123 ',
  20. '123'
  21. ),
  22. 'Cccccccc' => array (
  23. '123 ',
  24. '123 ',
  25. '123'
  26. ),
  27. ),
  28. 'Bbbbbbbb' => array (
  29. 'Aaaaa' => array (
  30. '123 ',
  31. '123 ',
  32. '123'
  33. ),
  34. 'Bbbbbbbb' => array (
  35. '123 ',
  36. '123 ',
  37. '123'
  38. ),
  39. 'Cccccccc' => array (
  40. '123 ',
  41. '123 ',
  42. '123'
  43. ),
  44. ),
  45. 'Cccccccc' => array (
  46. 'Aaaaa' => array (
  47. '123 ',
  48. '123 ',
  49. '123'
  50. ),
  51. 'Bbbbbbbb' => array (
  52. '123 ',
  53. '123 ',
  54. '123'
  55. ),
  56. 'Cccccccc' => array (
  57. '000000' => array ('44', '55', '66 '),
  58. '000000' => array ('44', '55', '66 '),
  59. '20140901' => array (
  60. '44' => array ('77 ', '88', '99 '),
  61. '55' => array ('77 ', '88', '99 '),
  62. '66 '=> array ('77', '88 ', '99 '),
  63. ),
  64. ),
  65. ),
  66. );

  67. // Execute the function

  68. Fun ($ );
  69. // The infinite classification recursion method starts.
  70. Function fun ($ _ info, $ deep = 0 ){
  71. // Determine if it is an array
  72. If (is_array ($ _ info )){
  73. // Foreach loop
  74. Foreach ($ _ info as $ key => $ val ){
  75. // Before the first time-no more than four additional output key names in each loop
  76. Echo str_repeat ('-', $ deep). $ key .'
    ';
  77. // Recursive output key value. By the way, four values are added each time ----
  78. Fun ($ val, $ deep + 4 );
  79. }
  80. } Else {
  81. // Return directly if the key value is not an array
  82. Echo str_repeat ('-', $ deep). "$ val
    ";
  83. }
  84. }
  85. ?>

Recursive interpretation: as an algorithm, recursion is widely used in programming languages. it refers to the re-import phenomenon generated when a function, process, or subprogram calls itself directly or indirectly during operation. Recursion is an important concept in computer science. the recursive method is an effective method in programming. writing a program using recursion can make the program concise and clear.

If recursion is not used, the execution efficiency is relatively low.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.