suryakala

Untitled

Mar 9th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. this is my php code
  2.  
  3. <?php
  4. $con=mysqli_connect("localhost","root","admin321","data");
  5.  
  6. if (mysqli_connect_errno())
  7. {
  8. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  9. }
  10.  
  11. $sql="SELECT * FROM phptab where value LIKE '%அகடம்%'";
  12. $r = mysqli_query($con,$sql);
  13. $data = array();
  14.  
  15. while($row = mysqli_fetch_assoc($r)) {
  16.  
  17. $data[] = $row;
  18. }
  19. function buildtree($src_arr, $parent_id = 0, $tree = array())
  20. {
  21. foreach($src_arr as $idx => $row)
  22. {
  23. if($row['parent'] == $parent_id)
  24. {
  25. foreach($row as $k => $v)
  26. $tree[$row['id']][$k] = $v;
  27. unset($src_arr[$idx]);
  28. $tree[$row['id']]['children'] = buildtree($src_arr, $row['id']);
  29. }
  30. }
  31. ksort($tree);
  32. return $tree;
  33. }
  34.  
  35. function insertIntoNestedArray(&$array, $searchItem){
  36.  
  37. if($searchItem['parent'] == 0){
  38. array_push($array, $searchItem);
  39. return;
  40. }
  41. if(empty($array)){ return; }
  42. array_walk($array, function(&$item, $key, $searchItem){
  43. if($item['id'] == $searchItem['parent']){
  44. array_push($item['children'], $searchItem);
  45. return;
  46. }
  47. insertIntoNestedArray($item['children'], $searchItem);
  48. }, $searchItem);
  49. }
  50. $nestedArray = array();
  51. foreach($data as $itemData){
  52. $nestedArrayItem['id'] = $itemData['id'];
  53. $nestedArrayItem['name'] = $itemData['name'];
  54. $nestedArrayItem['parent'] = $itemData['parent'];
  55. $nestedArrayItem['color'] = $itemData['color'];
  56. $nestedArrayItem['children'] = array();
  57. insertIntoNestedArray($nestedArray, $nestedArrayItem);
  58. }
  59. header('Content-Type: application/json');
  60. print_r($nestedArray);
  61. ?>
  62.  
  63. OUTPUT FOR THE ABOVE CODE IS THIS
  64.  
  65. Array
  66. (
  67. [0] => Array
  68. (
  69. [id] => 44
  70. [name] => அகடம்
  71. [parent] =>
  72. [color] => red
  73. [children] => Array
  74. (
  75. [0] => Array
  76. (
  77. [id] => 45
  78. [name] => மோசடி
  79. [parent] => 44
  80. [color] => red
  81. [children] => Array
  82. (
  83. )
  84.  
  85. )
  86.  
  87. [1] => Array
  88. (
  89. [id] => 46
  90. [name] => சூழ்ச்சி
  91. [parent] => 44
  92. [color] => red
  93. [children] => Array
  94. (
  95. )
  96.  
  97. )
  98.  
  99. [2] => Array
  100. (
  101. [id] => 47
  102. [name] => அநீதி
  103. [parent] => 44
  104. [color] => red
  105. [children] => Array
  106. (
  107. )
  108.  
  109. )
  110.  
  111. [3] => Array
  112. (
  113. [id] => 48
  114. [name] => பொல்லாங்கு
  115. [parent] => 44
  116. [color] => red
  117. [children] => Array
  118. (
  119. )
  120.  
  121. )
  122.  
  123. )
  124.  
  125. )
  126.  
  127. )
  128.  
  129.  
  130.  
  131. EXPECTED OUPUT
  132.  
  133. Array
  134. (
  135. [0] => Array
  136. (
  137. [id] => 44
  138. [name] => அகடம்
  139. [parent] =>
  140. [color] => red
  141. [children] => Array
  142. (
  143. [0] => Array
  144. (
  145. [id] => 45
  146. [name] => மோசடி
  147. [parent] => 44
  148. [color] => red
  149.  
  150.  
  151. )
  152.  
  153. [1] => Array
  154. (
  155. [id] => 46
  156. [name] => சூழ்ச்சி
  157. [parent] => 44
  158. [color] => red
  159.  
  160.  
  161. )
  162.  
  163. [2] => Array
  164. (
  165. [id] => 47
  166. [name] => அநீதி
  167. [parent] => 44
  168. [color] => red
  169.  
  170.  
  171. )
  172.  
  173. [3] => Array
  174. (
  175. [id] => 48
  176. [name] => பொல்லாங்கு
  177. [parent] => 44
  178. [color] => red
  179.  
  180.  
  181. )
  182.  
  183. )
  184.  
  185. )
  186.  
  187. )
  188.  
  189. DIGGERENCE IN THE EXPECTED OUPUT AND ORIGINAL OUPUT -
  190. REMOVED EMPTY VALUE FROM THE ARRAY
  191. [children] => Array
  192. (
  193. )
  194. )
Add Comment
Please, Sign In to add comment