Return to Snippet

Revision: 8485
at September 25, 2008 08:07 by jtkendall


Updated Code
<?php

	function recursion($multi_dimensional_array)
	{
		$m = $multi_dimensional_array;

		$keys = array();
		foreach($m as $key=>$value)
		{
			$keys[] = $key;
		}

		$i = 0;
		while($i < count($multi_dimensional_array))
		{
			echo '<li><a href="#">'.$keys[$i].'</a>';
			if(is_array($multi_dimensional_array[$keys[$i]]))
			{
				echo '<ul>';
				recursion($multi_dimensional_array[$keys[$i]]);
				echo '</ul>';
			}
			echo '</li>';
			$i++;
		}
	}

?>

Usage Example:

<ul>
	<? recursion($array); ?>
</ul>

Revision: 8484
at September 25, 2008 07:19 by jtkendall


Updated Code
<?php

	function recursion($multi_dimensional_array)
	{
		$m = $multi_dimensional_array;

		$keys = array();
		foreach($m as $key=>$value)
		{
			$keys[] = $key;
		}

		$i = 0;
		while($i < count($multi_dimensional_array))
		{
			echo '<li><a href="#">'.$keys[$i].'</a>';
			if(is_array($multi_dimensional_array[$keys[$i]]))
			{
				echo '<ul>';
				recurssion($multi_dimensional_array[$keys[$i]]);
				echo '</ul>';
			}
			echo '</li>';
			$i++;
		}
	}

?>

Usage Example:

<ul>
	<? recursion($array); ?>
</ul>

Revision: 8483
at September 25, 2008 07:18 by jtkendall


Initial Code
<?php

	function recurssion($multi_dimensional_array)
	{
		$m = $multi_dimensional_array;

		$keys = array();
		foreach($m as $key=>$value)
		{
			$keys[] = $key;
		}

		$i = 0;
		while($i < count($multi_dimensional_array))
		{
			echo '<li><a href="#">'.$keys[$i].'</a>';
			if(is_array($multi_dimensional_array[$keys[$i]]))
			{
				echo '<ul>';
				recurssion($multi_dimensional_array[$keys[$i]]);
				echo '</ul>';
			}
			echo '</li>';
			$i++;
		}
	}

?>

Usage Example:

<ul>
	<? recurssion($array); ?>
</ul>

Initial URL

                                

Initial Description
Can probably be done easier/better, but it accomplishes what I needed it to do. Figured I'd share.

Initial Title
Recursive UL Output with name-based array keys

Initial Tags

                                

Initial Language
PHP