Breadcrumbs

This one had me stumped!!

Working with arrays is something that every developer has to be comfortable with. Fortunately, PHP has mountains of array functions to offer.

Consider the following data:

<?php // here is some test data

$categories = array(
	array(
		'name'=>'Minerals'
	),
	array(
		'name'=>'Animals',
		'children'=>array(
			array('name'=>'Mammals'),
			array(
				'name'=>'Reptiles',
				'children'=>array(
					array('name'=>'Puffadder'),
					array('name'=>'Cape Cobra')
				)
			)
		)
	),
	array(
		'name'=>'Fruit',
		'children'=>array(
			array('name'=>'Citrus'),
			array(
				'name'=>'Deciduous',
				'children'=>array(
					array('name'=>'Pears'),
					array(
						'name'=>'Apples',
						'children'=>array(
							array('name'=>'Granny Smith'),
							array('name'=>'Star King')
						)
					)
				)
			)
		)
	)
);

Keeping in mind that the $categories can be nested any number of levels deep, write a script/function/class that can find a category by name and return its hierarchy in a folder like fashion.

For example searching for ‘Granny Smith’, the script would return ‘Fruit/Apples/Granny Smith’.

<?php
// here is some test data
$categories = array(
	array(
		'name'=>'Minerals'
	),
	array(
		'name'=>'Animals',
		'children'=>array(
			array('name'=>'Mammals'),
			array(
				'name'=>'Reptiles',
				'children'=>array(
					array('name'=>'Puffadder'),
					array('name'=>'Cape Cobra')						
				)
			)
		)
	),
	array(
		'name'=>'Fruit',
		'children'=>array(
			array('name'=>'Citrus'),
			array(
				'name'=>'Deciduous',
				'children'=>array(
					array('name'=>'Pears'),
					array(
						'name'=>'Apples',
						'children'=>array(
							array('name'=>'Granny Smith'),
							array('name'=>'Star King')
						)
					)
				)					
			)
		)
	)
);

// get the search value if it is POSTed
$needle = isset($_POST['iselect'])?$_POST['iselect']:'';

// form to input a search value
echo '<form action="recursive.php" method="POST">';
echo '<input name="iselect" value="'.$needle.'"/>';
echo '<input name="send" type="submit" />';
echo '</form>';

echo '<hr />';

function breadcrumb($tree, $needle, &$result = array()) {

    $result = array();

    if (is_array($tree)) {
        foreach ($tree as $node) {
            if ($node['name'] == $needle) {
                $result[] = $node['name'];
                return true;
            } else if (!empty($node['children'])) {
                if (breadcrumb($node['children'], $needle, $result)){
                $result[] = $node['name'];
                return true;
                }
            }
        }
    } else {
        if ($tree == $needle) {
            $result[] = $tree;
            return true;
        }
    }
    return false;
}

breadcrumb($categories, $needle, $result);

// reverse the array and output it as a breadcrumb-like path
print_r(implode(" >> ",array_reverse($result)));
?>

By foxbeefly

PHP / MySQL Developer

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.