Jump to content

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

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.