Jump to content

[SOLVED] Function not returning value


unxposed

Recommended Posts

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);

Link to comment
Share on other sites

		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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.