unxposed Posted August 2, 2009 Share Posted August 2, 2009 Hi guys, I've been struggling all day with this function and I think I'm there, it's just now I can't get the value of the array out of the function to use further down my page. Which I can print within the function and returns perfectly. I'd really appreciate a little help. Thanks. function folder_array($array_depth, $j, $sub_directory, $current_directory) { if ($j == 0) { foreach ($array_depth as $key => $value) { if ($value == $sub_directory[0]) { $array_depth[$sub_directory[0]] = array(); if ($handle = opendir($current_directory)) { while (false !== ($file = readdir($handle))) { if ((!strpos($file, '.')) && ($file !== '.') && ($file !== '..')) { $array_depth[$sub_directory[0]][$file] = $file; } } closedir($array_depth); } } } } elseif ($j == 1) { foreach ($array_depth[$sub_directory[0]] as $key => $value) { if ($value == $sub_directory[1]) { $array_depth[$sub_directory[0]][$sub_directory[1]] = array(); if ($handle = opendir($current_directory)) { while (false !== ($file = readdir($handle))) { if ((!strpos($file, '.')) && ($file !== '.') && ($file !== '..')) { $array_depth[$sub_directory[0]][$sub_directory[1]][$file] = $file; } } closedir($array_depth); } } } } elseif ($j == 2) { foreach ($array_depth[$sub_directory[0]][$sub_directory[1]] as $key => $value) { if ($value == $sub_directory[2]) { $array_depth[$sub_directory[0]][$sub_directory[1]][$sub_directory[2]] = array(); if ($handle = opendir($current_directory)) { while (false !== ($file = readdir($handle))) { if ((!strpos($file, '.')) && ($file !== '.') && ($file !== '..')) { $array_depth[$sub_directory[0]][$sub_directory[1]][$sub_directory[2]][$file] = $file; } } closedir($array_depth); } } } } $j++; if ($j < count($sub_directory)) { $return = folder_array($array_depth, $j, $sub_directory, $current_directory); } else { /* Test print the array - WORKING */ print_r($array_depth); /* Return the array */ return $array_depth; } } folder_array($form[folders], 0, $form[sub_directory], $form[current_directory]); /* Print the returned array - NOT WORKING */ print_r($return); Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/ Share on other sites More sharing options...
Philip Posted August 2, 2009 Share Posted August 2, 2009 if ($j < count($sub_directory)) { $return = folder_array($array_depth, $j, $sub_directory, $current_directory); } else { /* Test print the array - WORKING */ print_r($array_depth); /* Return the array */ return $array_depth; } Well, one sets a $return variable, and the other actually returns for the function. Which did you want? Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/#findComment-889112 Share on other sites More sharing options...
unxposed Posted August 3, 2009 Author Share Posted August 3, 2009 So what should be happening there is that that I have the fuction within itself, and it's repeated indefinately until that conditional is not true. Each time adding new values to the $array_depth array. When that conditional is not true then the return should be set inside the final function. Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/#findComment-889314 Share on other sites More sharing options...
unxposed Posted August 3, 2009 Author Share Posted August 3, 2009 I think it definately has something to do with the function looping within itself. Which of the functions do I need to define as the variable I wish top print further down the page $return. The first function, all of them, the last one? It seem logical that it would be the last one - which modifying the code here would produce: $j++; if (($j + 1) < count($sub_directory)) { folder_array($array_depth, $j, $sub_directory, $current_directory); } elseif ($j < count($sub_directory)) { $return = folder_array($array_depth, $j, $sub_directory, $current_directory); } else { /* Test print the array - WORKING */ print_r($array_depth); /* Return the array */ return array($array_depth, $current_directory); } But then again setting all of the functions as the same variable, I'd just assume the variable would keep getting overrided with the last function output. So that would be equally as logical. As I guess would be setting the first function only as the variable, as all other functions are within this! Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/#findComment-889339 Share on other sites More sharing options...
unxposed Posted August 3, 2009 Author Share Posted August 3, 2009 I guess to simply this to find out where I'm going wrong - I'm basically trying to get the value 'Hello world' out of the following function: function test($i) { $i++; if ($i == 1) { $return = test($i); } elseif ($i == 2) { $return = test($i); } else { return 'Hello world'; } } $return = test(0); echo $return; I've tried only naming the first as a variable, the last, all of them, giving them different variable names... but no luck! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/#findComment-889362 Share on other sites More sharing options...
unxposed Posted August 3, 2009 Author Share Posted August 3, 2009 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/#findComment-889624 Share on other sites More sharing options...
Philip Posted August 3, 2009 Share Posted August 3, 2009 I guess to simply this to find out where I'm going wrong - I'm basically trying to get the value 'Hello world' out of the following function: function test($i) { $i++; if ($i == 1) { $return = test($i); } elseif ($i == 2) { $return = test($i); } else { return 'Hello world'; } } $return = test(0); echo $return; I've tried only naming the first as a variable, the last, all of them, giving them different variable names... but no luck! Thanks! If you're trying to be recursive, you need to use the return value like so: <?php function test($i) { $i++; if ($i == 1) { return test($i); } elseif ($i == 2) { return test($i); } else { return 'Hello world'; } } $return = test(0); echo $return; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/#findComment-889973 Share on other sites More sharing options...
unxposed Posted August 3, 2009 Author Share Posted August 3, 2009 Oh my god yes yes yes! Thank you. It seems so obvious now... but I've spent hours and hours of my life trying to work that out! Quote Link to comment https://forums.phpfreaks.com/topic/168546-solved-function-not-returning-value/#findComment-890008 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.