rcouser Posted June 23, 2011 Share Posted June 23, 2011 Hello there, I am having problem returning array from function the code is as follows: $category_array = array(); function display_categories($parent, $level) { $conn = dbConnect('query'); $sql = "SELECT id, name FROM categories WHERE parent_id = $parent AND is_active = 1 ORDER BY sort_order DESC"; $result = @ $conn->query($sql); while($categories = $result->fetch_assoc()) { $array = array('id' => $categories['id'], 'name' => $categories['name'], 'level' => $level); display_categories($categories['id'], $level+1); } } $category_array = display_categories(0,0); var_dump($category_array); Any help much appreciated. Thank Quote Link to comment https://forums.phpfreaks.com/topic/240205-return-array-from-function/ Share on other sites More sharing options...
KevinM1 Posted June 23, 2011 Share Posted June 23, 2011 I don't see a return $array; statement anywhere.... Regardless, you're also overwriting your $array during each loop. You might be looking for: $array[] = array(/* stuff */); which would create a two dimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/240205-return-array-from-function/#findComment-1233824 Share on other sites More sharing options...
rcouser Posted June 23, 2011 Author Share Posted June 23, 2011 Thanks for the reply. I've changed it to the following but it only returns 1 result from the array. Any ideas? Cheers $category_array = array(); function display_categories($parent, $level) { $conn = dbConnect('query'); $sql = "SELECT id, name FROM categories WHERE parent_id = $parent AND is_active = 1 ORDER BY sort_order DESC"; $result = @ $conn->query($sql); while($categories = $result->fetch_assoc()) { $array[] = array('id' => $categories['id'], 'name' => $categories['name'], 'level' => $level); display_categories($categories['id'], $level+1); return $array; } } $category_array = display_categories(0,0); var_dump($category_array); Quote Link to comment https://forums.phpfreaks.com/topic/240205-return-array-from-function/#findComment-1233832 Share on other sites More sharing options...
KevinM1 Posted June 23, 2011 Share Posted June 23, 2011 You need to capture the results of your recursive call to: display_categories($categories['id'], $level+1); Quote Link to comment https://forums.phpfreaks.com/topic/240205-return-array-from-function/#findComment-1233854 Share on other sites More sharing options...
rcouser Posted June 23, 2011 Author Share Posted June 23, 2011 How would I go about that? Quote Link to comment https://forums.phpfreaks.com/topic/240205-return-array-from-function/#findComment-1233857 Share on other sites More sharing options...
KevinM1 Posted June 23, 2011 Share Posted June 23, 2011 How would I go about that? Well, that's really part of what you need to figure out during your design phase. I mean, technically, you just need to assign the function invocation to a variable, like: $var = display_categories($categories['id'], $level+1); Which will capture and place the returned result in $var. That said, you likely won't be getting the value(s) that you want due to the way your function is currently written. You'd still need to merge them with the $array you return. On top of that, you'll have to make sure everything lines up during recursion. Quote Link to comment https://forums.phpfreaks.com/topic/240205-return-array-from-function/#findComment-1233889 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.