Nuv Posted May 10, 2011 Share Posted May 10, 2011 My tree gather function gives me multidimensional array. However i want it to be one dimensional . I was getting one dimensional earlier however i forgot how i got it. Can someone please tell me my mistake Code <?php include'config.php'; function tree_gather($node) { $sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'"; $execsql = mysql_query($sql); $array = mysql_fetch_array($execsql); if(!empty($array['lchild']) || !empty($array['rchild'])) { $child[] = $array['lchild']; $child[] = $array['rchild']; $eh[] = tree_gather($array['lchild']); $child = array_merge($child, $eh); $eh1[] = tree_gather($array['rchild']); $child = array_merge($child, $eh1); } return $child; } $pair = tree_gather(1000000); echo "<pre>"; print_r($pair); echo "</pre>"; ?> Output Array ( [0] => 3632873 [1] => 5951538 [2] => Array ( [0] => 8930480 [1] => 7563232 [2] => Array ( [0] => 1144744 [1] => 4386810 [2] => [3] => ) [3] => ) [3] => ) Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/ Share on other sites More sharing options...
dreamwest Posted May 10, 2011 Share Posted May 10, 2011 Take the brackets away, because $array['lchild'] is a string $child = array($array['lchild']); print_r($child); //output example: array( [0] => 3632873 ) Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213298 Share on other sites More sharing options...
Nuv Posted May 10, 2011 Author Share Posted May 10, 2011 Did so with same multidimensional array. I believe the below code needs changes to make it onedimensional $eh[] = tree_gather($array['lchild']); $child = array_merge($child, $eh); $eh1[] = tree_gather($array['rchild']); $child = array_merge($child, $eh1); Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213300 Share on other sites More sharing options...
Nuv Posted May 10, 2011 Author Share Posted May 10, 2011 Still can't figure it out Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213454 Share on other sites More sharing options...
wildteen88 Posted May 10, 2011 Share Posted May 10, 2011 Its due to $eh[] and $eh1[]. Get rid of the square brackets. The following should do it. $lchild = tree_gather($array['lchild']); $rchild = tree_gather($array['rchild']); $child = array_merge($lchild, $rchild); Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213462 Share on other sites More sharing options...
Nuv Posted May 10, 2011 Author Share Posted May 10, 2011 Its giving me warnings. Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\Users\.php on line 17 Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:xxx\xxx\xxx.php on line 17 Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\xxx\xxx\xxx.php line 17 Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:\xxx\xxx\xxx.php on line 17 Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\xxx\xxx\xxx.php on line 17 Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:\xxx\xxx\xxx.php on line 17 Line 17 being $child = array_merge($lchild, $rchild); Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213465 Share on other sites More sharing options...
Nuv Posted May 10, 2011 Author Share Posted May 10, 2011 Thus i used [] in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213471 Share on other sites More sharing options...
btherl Posted May 11, 2011 Share Posted May 11, 2011 Recursion always has a base case and a recursive case. You need to be clear on what to do for each one. 1. Base case - the tree node has no children. What action to take? Is the correct action to return an empty array? 2. Recursive case - the tree node has a left child. What action to take? Process left child, keep results in an array, and .. 3. Recursive case - the tree node has a right child. What action to take? Process right child, keep results in an array, and combine with any results from recursive case 2. Then return combined results. Now the questions I have for you: 1. What does your code do if the node has no children? What value does it return? It should be an empty array, written "array()", which is different from an uninitialized variable. PHP won't like it if you try to merge an uninitialized variable with an array. 2. What does your code do if the left child exists but the right child doesn't? It should process just the left child if only the left child exists. Same with the right child. The problem with the arrays being inside arrays is because you used $eh[] instead of $eh, and $eh1[] instead of $eh1. wildteen is correct there, and adding the brackets will only give the wrong results, it doesn't actually fix the problem. The actual problem was that your function does not always return an array, it sometimes returns an unintialized $child. Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213671 Share on other sites More sharing options...
Nuv Posted May 11, 2011 Author Share Posted May 11, 2011 Thank you for the explanation btherl. I made a new function now. I added levels to the ID too . However my function wont retrieve the last entry in the table having no rchild and have just lchild. Function attached with the image of the table. My function wont add '9567182' to the array. <?php function tree_gather($node, $level) { $sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'"; $execsql = mysql_query($sql); $array = mysql_fetch_array($execsql); if(!empty($array['lchild']) || !empty($array['rchild'])) { if(!empty($array['lchild'])) $child[] = $array['lchild']."_".$level; if(!empty($array['rchild'])) $child[] = $array['rchild']."_".$level; if(!empty($array['lchild'])) $lchild = tree_gather($array['lchild'], $level + 1); if(!empty($array['rchild'])) $rchild = tree_gather($array['rchild'], $level + 1); if(!empty($lchild) && !empty($rchild)) { $merge_both = array_merge($lchild, $rchild); $child = array_merge($child, $merge_both); } elseif(!empty($lchild) && empty($rchild)) $child = array_merge($child, $lchild); elseif(!empty($rchild) && empty($lchild)) $child = array_merge($child, $rchild); } return $child; } ?> Output Array ( [0] => 3632873_1 [1] => 5951538_1 [2] => 8930480_2 [3] => 7563232_2 [4] => 1144744_3 [5] => 4386810_3 [6] => 1236598_2 [7] => 2305948_2 [8] => 9523654_3 [9] => 5692147_3 ) Table Image Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213751 Share on other sites More sharing options...
Nuv Posted May 11, 2011 Author Share Posted May 11, 2011 Forgive me for my silly mistake. There are two parents with same ID which isnt possible. My code works perfectly now. Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1213887 Share on other sites More sharing options...
btherl Posted May 11, 2011 Share Posted May 11, 2011 Congratulations Recursion is tricky but once you get used to it it's very useful. Quote Link to comment https://forums.phpfreaks.com/topic/236004-want-my-function-to-return-one-dimensional-array/#findComment-1214135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.