thugsr Posted July 31, 2013 Share Posted July 31, 2013 Hi, I have problem with recursive function. I want to store value form that function into array because i need to reverse result of function. Here is function function display_children($category_id, $level) { global $database; $result = mysql_query("SELECT * FROM parents WHERE id_roditelja='$category_id'") or die(mysql_error()); $broj = $database->num_rows($result); $niz = array(); while ($row = mysql_fetch_array($result)) { echo str_repeat(' ',$level) . $row['naziv'] . "-> "; $niz = display_children($row['parent_id'], $level+1); } } I just want to save "display_children($row['parent_id'], $level+1)" this into an array reverse it and then return it. Can someone help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/280667-write-value-from-function-into-array/ Share on other sites More sharing options...
cyberRobot Posted July 31, 2013 Share Posted July 31, 2013 For adding values to an array, take a look at the "Creating/modifying with square bracket syntax" section found here: http://php.net/manual/en/language.types.array.php The section is right after example 7. Quote Link to comment https://forums.phpfreaks.com/topic/280667-write-value-from-function-into-array/#findComment-1442784 Share on other sites More sharing options...
thugsr Posted August 1, 2013 Author Share Posted August 1, 2013 That don't work for my case... any other suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/280667-write-value-from-function-into-array/#findComment-1442918 Share on other sites More sharing options...
cyberRobot Posted August 1, 2013 Share Posted August 1, 2013 (edited) What code did you try? We may be able to help you better by seeing the code. Edited August 1, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/280667-write-value-from-function-into-array/#findComment-1442948 Share on other sites More sharing options...
thugsr Posted August 1, 2013 Author Share Posted August 1, 2013 In my first post is code of function, on that i changed line $niz[] = display_children($row['parent_id'], $level+1); like this,and in other variation i try with for loop where counter $i was key in array, that isn't worked as well...i need this to be stored into array display_children($row['parent_id'], $level+1); Quote Link to comment https://forums.phpfreaks.com/topic/280667-write-value-from-function-into-array/#findComment-1442953 Share on other sites More sharing options...
cyberRobot Posted August 1, 2013 Share Posted August 1, 2013 (edited) That ($niz[]) should give you an array. To see for yourself, you could temporarily add the following line of code after the while loop: <?php echo '<pre>' . print_r($niz, true) . '</pre>'; ?> From there you should look into array_reverse(): http://php.net/manual/en/function.array-reverse.php ...and the return statement: http://php.net/manual/en/function.return.php Edited August 1, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/280667-write-value-from-function-into-array/#findComment-1442967 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.