Jump to content

Recursive function


sKunKbad

Recommended Posts

Just getting tired, and don't see my problem. Output should be:

 

/Examples/Widgets/Hardcore

 

but all I am getting is:

 

/Hardcore

 

Code Example:

 

<?php

$all_categories_array = array(
'0' => array(
			'category_id' => '1',
			'category_name' => 'Examples',
			'parent_id' => '0'
),
'1' => array(
			'category_id' => '2',
			'category_name' => 'Widgets',
			'parent_id' => '1'
),
'2' => array(
			'category_id' => '3',
			'category_name' => 'Little',
			'parent_id' => '1'
),
'3' => array(
			'category_id' => '4',
			'category_name' => 'Hardcore',
			'parent_id' => '2'
),
);


function get_product_parents($id, $all_categories_array, $steps)
{

foreach($all_categories_array as $x => $y)
{
	// if this is the product's category
	if($y['category_id'] == $id)
	{
		// add the category name to the steps array
		$steps[] = $y['category_name'];

		// and do recursion if not a top level category
		if($y['parent_id'] != 0)
		{
			get_product_parents($y['parent_id'], $all_categories_array, $steps );
		}
	}
}

$reverse_array = array_reverse($steps);
$path = '';
foreach($reverse_array as $category_name)
{
	$path .= '/' . $category_name;
}

return $path;
}

$steps = array();
$link_path = get_product_parents('4', $all_categories_array, $steps);

// Should be /Examples/Widgets/Hardcore
echo $link_path;

Link to comment
https://forums.phpfreaks.com/topic/204073-recursive-function/
Share on other sites

This actually works, as I can echo $path, but if I try to return $path it is empty:

 

<?php

$all_categories_array = array(
'0' => array(
			'category_id' => '1',
			'category_name' => 'Examples',
			'parent_id' => '0'
),
'1' => array(
			'category_id' => '2',
			'category_name' => 'Widgets',
			'parent_id' => '1'
),
'2' => array(
			'category_id' => '3',
			'category_name' => 'Little',
			'parent_id' => '1'
),
'3' => array(
			'category_id' => '4',
			'category_name' => 'Hardcore',
			'parent_id' => '2'
),
);


function get_product_parents($id, $all_categories_array, $steps)
{
// check the entire categories array for a matching category
foreach($all_categories_array as $x => $y)
{
	// if this is the product's category
	if($y['category_id'] == $id)
	{
		// add the category name to the steps array
		$steps[] = $y['category_name'];

		// and do recursion if not a top level category
		if($y['parent_id'] !== '0')
		{
			get_product_parents($y['parent_id'], $all_categories_array, $steps );
			exit;
		}
		else
		{
			$reverse_array = array_reverse($steps);
			$path = '';
			foreach($reverse_array as $category_name)
			{
				$path .= '/' . $category_name;
			}

			// Should be /Examples/Widgets/Hardcore
			echo $path;
		}
	}
}

}

$steps = array();
$whatever = get_product_parents('4', $all_categories_array, $steps);
var_dump($whatever);

Link to comment
https://forums.phpfreaks.com/topic/204073-recursive-function/#findComment-1068886
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.