Known table t1 {code ...} you want to query {code ...} data is rearranged by php {code ...} get the data in the following structure {code ...} in this process, how should I write PHP code or algorithms? Please advise. Known table t1
Id txt parent -------------------------- 1 hitting a tiger in the mountains 0 2 1234 5 0 3 the tiger is not at home 1 4 La la ah 2 6 ah n en 0...
You want to query
mysql_query("SELECT id,txt,parent FROM t1 ORDER BY id DESC")
Data is rearranged by php
How should I write the code here ???
Obtain the data in the following structure:
- Well, nun
- September 5
- Fight tigers in the mountains
- La la
- The tiger is not at home.
In this process, how should I write PHP code or algorithms? Please advise.
Reply content:
Known table t1
Id txt parent -------------------------- 1 hitting a tiger in the mountains 0 2 1234 5 0 3 the tiger is not at home 1 4 La la ah 2 6 ah n en 0...
You want to query
mysql_query("SELECT id,txt,parent FROM t1 ORDER BY id DESC")
Data is rearranged by php
How should I write the code here ???
Obtain the data in the following structure:
- Well, nun
- September 5
- Fight tigers in the mountains
- La la
- The tiger is not at home.
In this process, how should I write PHP code or algorithms? Please advise.
Array ([1] => Web Server [3] => Nginx [4] => Apache) [2] => Array ([2] => Database [5] => MySQL) [6] => Array ([6] => Browser ))
Code
/*** Infinite sublevel recursive loop * @ param [type] $ data [description] * @ param integer $ pid [description] * @ return [type] [description] */ function unlimitedForChild ($ data, $ pid = 0) {$ array = array (); foreach ($ data as $ value) {if ($ value ['parent'] = $ pid) {$ value ['child '] = unlimitedForChild ($ data, $ value ['id']); $ array [] = $ value ;}} return $ array ;} $ data = array ('id' => 1, 'txt '=> 'Go up the client', 'parent' => 0 ), array ('id' => 2, 'txt '=> 'August 5', 'parent' => 0), array ('id' => 3, 'txt '=> 'tiger not at home', 'parent' => 1), array ('id' => 4, 'txt '=> 'la la Lala ', 'parent' => 1), array ('id' => 5, 'txt '=>', 'parent' => 2 ), array ('id' => 6, 'txt '=> 'hmm nen', 'parent' => 0 ), // append Level 3 array ('id' => 7, 'txt '=> 'level 3', 'parent' => 3 ),); $ result = unlimitedForChild ($ data); echo'';print_r($result);echo '
';
Result
Array ([0] => Array ([id] => 1 [txt] => hitting a tiger in the mountains [parent] => 0 [child] => Array ([0] => array ([id] => 3 [txt] => tiger is not at home [parent] => 1 [child] => Array ([0] => Array ([id] => 7 [txt] => Level 3 [parent] => 3 [child] => Array ()))) [1] => Array ([id] => 4 [txt] => La la [parent] => 1 [child] => Array ()))) [1] => Array ([id] => 2 [txt] => February 5, 1234 [parent] => 0 [child] => Array ([0] => Array ([id] => 5 [txt] => ah [parent] => 2 [child] => Array ()))) [2] => Array ([id] => 6 [txt] => hmm nun [parent] => 0 [child] => Array ()))
Array ( [name] => Web Server [sub] => Array ( [3] => Array ( [name] => Nginx ) [4] => Array ( [name] => Apache ) ) ) [2] => Array ( [name] => Database [sub] => Array ( [5] => Array ( [name] => MySQL ) ) ) [6] => Array ( [name] => Browser ))