Nuv Posted May 2, 2011 Share Posted May 2, 2011 I am trying to store all the elements of my tree in an array.My code produces multi dimensional array, but i want it to be one dimensional. How can i get it ? Code:- function tree_gather($node) //Function to calculate count { $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']; $child[] = tree_gather($array['lchild']); $child[] = tree_gather($array['rchild']); } return $child; } Result:- Array ( [0] => 2 [1] => 3 [2] => Array ( [0] => 4 [1] => 5 [2] => Array ( [0] => 10 [1] => 11 [2] => [3] => ) [3] => Array ( [0] => 8 [1] => 9 [2] => [3] => ) ) [3] => Array ( [0] => 7 [1] => 6 [2] => Array ( [0] => 15 [1] => 14 [2] => Array ( [0] => 16 [1] => 17 [2] => [3] => ) [3] => ) [3] => Array ( [0] => 13 [1] => 12 [2] => [3] => ) ) ) Table: - Link to comment https://forums.phpfreaks.com/topic/235351-how-to-get-one-dimensional-array-and-not-multidimensional-array/ Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 These line are causing your array to be multidimensional $child[] = tree_gather($array['lchild']); $child[] = tree_gather($array['rchild']); You may want to use array_merge here. Rather than assign the result of tree_gather() to your $child array. As you have it at the mement you will always get a multidimensional array. Link to comment https://forums.phpfreaks.com/topic/235351-how-to-get-one-dimensional-array-and-not-multidimensional-array/#findComment-1209473 Share on other sites More sharing options...
Nuv Posted May 2, 2011 Author Share Posted May 2, 2011 Thankyou. Link to comment https://forums.phpfreaks.com/topic/235351-how-to-get-one-dimensional-array-and-not-multidimensional-array/#findComment-1209531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.