PHP recursive traversal and array practice

Source: Internet
Author: User
The essence of recursion is to return the result after zero vertices exist. If there is no zero point, an infinite loop will occur. Therefore, how to judge whether zero point is an important point for the successful use of recursion is provided below.

The essence of recursion is to return the result after zero vertices exist. If there is no zero point, an infinite loop will occur. So how to judge whether zero point is a key point for the successful use of recursion? the examples I have given below clearly comment on the zero point. Secondly, we need to understand the recursive process trend. In my summary, the following code goes into depth and then pops up. we can calculate the number of times the function has been executed. The following is an example of this problem.

Flat view printing?
01 // Calculate the factorial of N by recursive traversal of a simple array
03 If (strtolower ($ _ SERVER ['request _ method']) = 'post ')
04 {
05 $ Num = intval ($ _ REQUEST ['Number']);
06 If ($ num <0)
07 {
08 Exit ('Array is invalid! ');
09 }
10 If ($ num = 0)
11 {
12 $ Result = 1;
13 }
14 Else
15 {
16 $ Result = recursive ($ num );
17 }
18 Echostrval ($ num). "the factorial is:". $ result;
19 }
20 Else
21 {
22 Echo "enter a number in the text box above! ";
23 }
24 Functionrecursive ($ n)
25 {
26 If ($ n = 1) return $ n; // The zero point is displayed.
27 Return $ n * recursive ($ n-1); // here it is in-depth
28 }
29 // The zero point of the factorial is a process of n-> 1-> n.
30 Echo"
31
32
33
34 ";
35 // Recursive infinite classification of arrays
36 $ Arr = array ('id' => 1, 'name' => 'News Center', 'Father '=> 0, 'Deep' => 1 ),
37 Array ('id' => 2, 'name' => 'domestic Center', 'Father '=> 1, 'Deep' => 2 ),
38 Array ('id' => 3, 'name' => 'Entertainment News ', 'Father' => 0, 'Deep '=> 1 ),
39 Array ('id' => 4, 'name' => 'Sports News ', 'Father' => 3, 'Deep '=> 2 ),
40 Array ('id' => 5, 'name' => 'start', 'Father '=> 4, 'Deep' => 3 ),
41 Array ('id' => 6, 'name' => 'Beijing News ', 'Father' => 1, 'Deep '=> 2 ),
42 Array ('id' => 7, 'name' => 'Beijing', 'Father '=> 3, 'Deep' => 2 ));
43 // Id => id
44 // Name => name
45 // Father => parent ID
46 // Deep => topic depth
47 FunctionUnlimitedclass ($ arr = array (), $ fid = 0)
48 {
49 // Static array must be used for array implementation; otherwise, the value will be lost.
50 Static $ results = array ();
51 If (is_array ($ arr) & $ arr)
52 {
53 Foreach ($ arras $ k => $ v)
54 {
55 // The zero point is the last node of all top-level columns.
56 If ($ v ['Father '] = $ fid)
57 {
58 $ Results [$ v ['id'] = array ('id' => $ v ['id'], 'name' => $ v ['name'], 'deep '=> $ v ['deep']);
59 // Recursive call here is in-depth
60 Unlimitedclass ($ arr, $ v ['id']);
61 }
62 }
63 // Return result set
64 Return $ results;
65 }
66 }
67 $ Re = Unlimitedclass ($ arr );
68 If (is_array ($ re) & $ re)
69 {
70 Foreach ($ reas $ k => $ v)
71 {
72 Echostr_repeat ('', $ v ['deep ']). $ v ['name'].'
73 ';
74 }
75 }
76 // A static variable is defined in the above example. The so-called static variable is that the value will not be lost after leaving the function;
77 // The Monkey King election is also a classic array operation
78 // Give you a number of N. remove a monkey every M times and keep repeating until the last one is left. then the monkey is the king.
79 $ Monkeys = range (1, 10); // monkey ID
80 $ J = 3; // remove one for every 3
81 For ($ I = 0; count ($ monkeys)> 1; $ I ++)
82 {
83 If ($ I + 1) % $ j = 0)
84 {
85 Unset ($ monkeys [$ I]);
86 }
87 Else
88 {
89 Array_push ($ monkeys, $ monkeys [$ I]);
90 Unset ($ monkeys [$ I]);
91 }
92 }
93 Print_r ($ monkeys );

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.