An example of an infinite loop array (recursion)

WBOY
Release: 2016-07-25 09:04:48
Original
1611 people have browsed it
  1. /**

  2. * Author : GuoWangYunYan
  3. * QQ : 279861795
  4. * Date : 2011-6-23
  5. * link:www.jbuxe.com
  6. */
  7. //Set encoding
  8. header('Content-type: text/html; charset=utf-8');
  9. / /In a weird way, I used a five-dimensional array
  10. $a = array(
  11. 'AAAAAA' => array(
  12. 'aaaaaa' => array(
  13. '111111',
  14. '222222',
  15. '333333'
  16. ) ,
  17. 'bbbbbb' => array(
  18. '111111',
  19. '222222',
  20. '333333'
  21. ),
  22. 'cccccc' => array(
  23. '111111',
  24. '222222',
  25. '333333'
  26. ),
  27. ),
  28. 'BBBBBB' => array(
  29. 'aaaaaa' => array(
  30. '111111',
  31. '222222',
  32. '333333'
  33. ),
  34. 'bbbbbb' => array(
  35. '111111',
  36. '222222',
  37. '333333'
  38. ),
  39. 'cccccc'=> array(
  40. '111111',
  41. '222222',
  42. '333333'
  43. ),
  44. ),
  45. 'CCCCCC' => array(
  46. 'aaaaaa'=> array(
  47. '111111',
  48. '222222',
  49. '333333'
  50. ),
  51. 'bbbbbb' => array(
  52. '111111',
  53. '222222',
  54. '333333'
  55. ),
  56. 'cccccc' => array(
  57. '111111'=>array('44','55','66'),
  58. '222222'=>array('44' ,'55','66'),
  59. '333333'=>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 function

  68. fun($a);
  69. //The infinite classification recursion method begins
  70. function fun ($_info,$deep=0){
  71. //Judge whether it is an array
  72. if ( is_array($_info)){
  73. //foreach loop
  74. foreach ($_info as $key=>$val){
  75. //The first time before - no more each time the loop adds 4 by the way to output the key name
  76. echo str_repeat (' - ',$deep).$key.'
    ';
  77. //Recursively output the key value and add 4 in front each time----
  78. fun($val,$deep+4) ;
  79. }
  80. } else {
  81. //If the key value is not an array, return directly
  82. echo str_repeat('-', $deep) . "$val
    ";
  83. }
  84. }
  85. ?>< /p>
Copy code

Recursive explanation: Recursion, as an algorithm, is widely used in programming languages. It refers to the reentrancy phenomenon caused by a function/process/subprogram calling itself directly or indirectly during operation. Recursion is an important concept in computer science. The recursive method is an effective method in programming. Writing programs using recursion can make the program concise and clear.

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



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template